C#如何将list<Class>数据导入到Excel 你看下这个网站希望对你有用如果你还有什么不明白可以写信给我:http://www.cnblogs.com/northeastTycoon这个代码需要你下载:Npoi动态库。代码我都是经过测试的没有问题。对了在官网上就可以下载。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个主要是看,你class 的结构..一个for循环就可以导入到EXcel 了.... http://blog.csdn.net/happy09li/article/details/7431967 怎么不给分啊。把list<class>转为datatable就可以了。 各位前辈。。我这样导入数据,就会报错,可是我的逻辑没有错啊...请各位帮忙... 错误:“未处理的COMException”异常来自 HRESULT:ox800a03ec private static void ExportToExcel(string path,List<ExcelClass> listEclass) { object Nothing = System.Reflection.Missing.Value; //占位符 Excel.Application app = null; Excel.Workbook MyBook = null; Excel.Worksheet mySheet = null; app = new Excel.Application(); MyBook = app.Workbooks.Open(path, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing); mySheet = (Worksheet)MyBook.Worksheets[1]; for (int i = 0; i < listEclass.Count;i++) { mySheet.Cells[i, 1] = listEclass[i].AppID.ToString().Trim(); mySheet.Cells[i, 2] = listEclass[i].AccountName.ToString().Trim(); mySheet.Cells[i, 3] = listEclass[i].EmailDomain.ToString().Trim(); } MyBook.Save(); MyBook.Saved = true; if(mySheet!=null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet); mySheet=null; } if(MyBook!=null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(MyBook); MyBook = null; } //关闭对象 app.Workbooks.Close(); app.Quit(); if(app!=null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; } GC.Collect(); } ==============================检查你的 path参数。注意 \ 必须转义为\\ 或是前面加@ //注意必须是\\ 或是 @\不然会提示错误 sw.SaveAs("c:\a.xls");【错误!!】 sw.SaveAs(@"c:\a.xls“)【正确】 【C#】怎样到远程服务器上查询,增加,修改xml文件的内容,急,急,急 如何在Form1中呈现WPF的三维图像 C#事件难题,太有挑战性了 如何用for循环给控件的属性赋值 [100F]求教如何获取本程序集的guid值,或获取其他程序集的guid值呢?? 在form2中如何点击关闭按钮时,让它关闭整个程序? 请教点基础问题?C/S开发用什么工具好? 菜鸟求助!!! 多线程问题 紧急!!!! 如何把一个本地msg Outlook mail 文件转换成HTM格式? 求一段C# code Web Service向Client推送一个通知信息 新手学习 C#控制台程序_邮箱地址验证
一个for循环就可以导入到EXcel 了....
异常来自 HRESULT:ox800a03ec
private static void ExportToExcel(string path,List<ExcelClass> listEclass)
{
object Nothing = System.Reflection.Missing.Value; //占位符
Excel.Application app = null;
Excel.Workbook MyBook = null;
Excel.Worksheet mySheet = null; app = new Excel.Application(); MyBook = app.Workbooks.Open(path, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
mySheet = (Worksheet)MyBook.Worksheets[1]; for (int i = 0; i < listEclass.Count;i++)
{
mySheet.Cells[i, 1] = listEclass[i].AppID.ToString().Trim();
mySheet.Cells[i, 2] = listEclass[i].AccountName.ToString().Trim();
mySheet.Cells[i, 3] = listEclass[i].EmailDomain.ToString().Trim();
}
MyBook.Save();
MyBook.Saved = true; if(mySheet!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
mySheet=null;
}
if(MyBook!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(MyBook);
MyBook = null;
}
//关闭对象
app.Workbooks.Close();
app.Quit();
if(app!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
}
GC.Collect();
}
检查你的 path参数。
注意 \ 必须转义为\\ 或是前面加@
//注意必须是\\ 或是 @\不然会提示错误
sw.SaveAs("c:\a.xls");【错误!!】
sw.SaveAs(@"c:\a.xls“)【正确】