• 数据导到excel时出现Exception from HRESULT: 0x800A03EC的异常


    运行如下代码时发现弹出Exception from HRESULT: 0x800A03EC异常

            private void ExportExcel(DataTable dt)
            {
                if (dt == null) return;
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                if (xlApp == null)
                {
                    return;
                }
                System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                Microsoft.Office.Interop.Excel.Range range;
                long totalCount = dt.Rows.Count;
                long rowRead = 0;
                float percent = 0;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
                    range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
                    range.Interior.ColorIndex = 15;
                    range.Font.Bold = true;
                }
                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
                    }
                    rowRead++;
                    percent = ((float)(100 * rowRead)) / totalCount;
                }
                xlApp.Visible = true;
            }

    后Debug到worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];当数据含有"="后就弹出这个异常,把这名改为worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString().Replace("=","");后运行正常,特记录下来。

  • 相关阅读:
    HTTP协议
    浏览器兼容性问题
    面试笔试重点总结:操作系统、计算机网络、设计模式
    笔试、面试重点总结:算法基础、数据结构
    笔试、面试重点总结:WIN32、MFC与Linux
    《Java程序员面试笔试宝典》终于在万众期待中出版啦~
    《Java程序员面试笔试宝典》之Static关键字有哪些作用
    《Java程序员面试笔试宝典》之字符串创建与存储的机制是什么
    《Java程序员面试笔试宝典》之为什么需要public static void main(String[] args)这个方法
    《Java程序员面试笔试宝典》之Java变量命名有哪些规则
  • 原文地址:https://www.cnblogs.com/springyangwc/p/1956304.html
Copyright © 2020-2023  润新知