C# 导出EXCEL报错


初步判断可能原因为

1。WPS相关冲突

2。系统更新冲突


部分客户端重装MICROSOFT OFFICE后可以正常使用。

部分客户端卸载WPS OFFICE后重装MICROSOFT OFFICE后可以正常使用。

仍有部分客户端以上方法都无效。


临时解决方案,采用第三方组件NPOI


        public static void DataToExcelByNPOI(DataGridView dgv)
        {
            try
            {
                int rowCount = dgv.Rows.Count;
                int colCount = dgv.Columns.Count;

                //行数列数必须大于0    
                if (rowCount <= 0 || colCount <= 0)
                {
                    MessageBox.Show("没有数据可供导出!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                NPOI.HSSF.UserModel.HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
                MemoryStream ms = new MemoryStream();

                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("Sheet1");

                try
                {
                    //填入表头
                    NPOI.SS.UserModel.IRow dataHeadRow = sheet.CreateRow(0);
                    for (int j = 0; j < colCount; j++)
                    {
                        if (dgv.Columns[j].Visible && dgv.Columns[j].HeaderText != "")
                        {
                            NPOI.SS.UserModel.ICell cell = dataHeadRow.CreateCell(j);
                            cell.SetCellValue(dgv.Columns[j].HeaderText.ToString());
                        }
                    }
                    //填入明细
                    for (int i = 0; i < rowCount; i++)
                    {
                        NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(i+1);
                        for (int j = 0; j < colCount; j++)
                        {
                            if (dgv.Columns[j].Visible && dgv.Rows[i].Cells[j].Value != null)
                            {
                                NPOI.SS.UserModel.ICell cell = dataRow.CreateCell(j);
                                cell.SetCellValue(dgv.Rows[i].Cells[j].Value.ToString());
                            }
                        }
                    }

                    workbook.Write(ms);
                    SaveFileDialog sfd = new SaveFileDialog();

                    sfd.Filter = "Excel 97-2003工作薄(*.xls)|*.xls";
                    sfd.DefaultExt = ".xls";
                    sfd.RestoreDirectory = true;
                    sfd.OverwritePrompt = true;
                    sfd.Title = "保存文件";
                    if (DialogResult.OK != sfd.ShowDialog())
                    {
                        return;
                    }

                    string strFileName = sfd.FileName;

                    if (System.IO.File.Exists(strFileName))
                    {
                        System.IO.File.Delete(strFileName);
                    }
                    FileStream file = new FileStream(strFileName, FileMode.Create);
                    workbook.Write(file);
                    file.Close();
                    MessageBox.Show("保存EXCE成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    workbook = null;
                    ms.Close();
                    ms.Dispose();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


NPOI的下载地址:NPOI


你可能感兴趣的:(C# 导出EXCEL报错)