本文介绍了在C#中将数据导出到Excell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我使用此代码excelApp = 新 Microsoft.Office.Interop.Excel.Application(); excelBook = excelApp.Workbooks.Add(XlSheetType.xlWorksheet); excelWorksheet =(Worksheet)(excelBook.Worksheets [ 1 ]);; excelApp.Visible = true ; excelWorksheet.DisplayRightToLeft = true ; rng1 = excelWorksheet.get_Range(" A1" ," A1"); rng1.Value2 = " __ ID __" ; rng1.Font.Bold = true ; rng2 = excelWorksheet.get_Range(" B1" ," B1"); rng2.Value2 = " ID" ; rng2.Font.Bold = true ; rng3 = excelWorksheet.get_Range(" C1" ," C1"); rng3.Value2 = " 标题" ; rng3.Font.Bold = true ; i = 1 ; foreach (DataRow r 在 ds.Tables [ 0 ].Row) { i ++; 字符串 s = i.ToString(); 范围r1 = excelWorksheet.get_Range(" A" + s," A" + s); r1.Value2 = r [ 0 ].ToString(); 范围r2 = excelWorksheet.get_Range(" B" + s," B" + s); r2.Value2 = r [ 1 ].ToString(); 范围r3 = excelWorksheet.get_Range(" C" + s," C" + s); r3.Value2 = r [ 2 ].ToString(); }
用于将数据导出到Excell.
一段时间,我有一个错误!
"来自HRESULT:0x800AC472的异常""
请帮助我!
解决方案
hii use this code
excelApp = new Microsoft.Office.Interop.Excel.Application(); excelBook = excelApp.Workbooks.Add(XlSheetType.xlWorksheet); excelWorksheet =(Worksheet)(excelBook.Worksheets[1]); excelApp.Visible = true; excelWorksheet.DisplayRightToLeft = true; rng1 = excelWorksheet.get_Range("A1", "A1"); rng1.Value2 = "__ID__"; rng1.Font.Bold = true; rng2 = excelWorksheet.get_Range("B1", "B1"); rng2.Value2 = "ID"; rng2.Font.Bold = true; rng3 = excelWorksheet.get_Range("C1", "C1"); rng3.Value2 = "Title"; rng3.Font.Bold = true; i = 1; foreach (DataRow r in ds.Tables[0].Rows) { i++; string s = i.ToString(); Range r1 = excelWorksheet.get_Range("A" + s, "A" + s); r1.Value2 = r[0].ToString(); Range r2 = excelWorksheet.get_Range("B" + s, "B" + s); r2.Value2 = r[1].ToString(); Range r3 = excelWorksheet.get_Range("C" + s, "C" + s); r3.Value2 = r[2].ToString(); }
for Export data to Excell.
some time ,i have an Error!
"""Exception from HRESULT:0x800AC472"""
pleas help me!!
解决方案
这篇关于在C#中将数据导出到Excell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!