本博客还有大量的.NET开源技术文章,您可能感兴趣: 

1.开源Math.NET基础数学类库使用系列文章链接

2.开源C#彩票数据资料库系列文章链接

3.开源的.NET平台ORM组件文章:链接

4.其他开源的.NET组件文章:链接

5..NET平台机器学习组件-Infer.NET系列文章:链接

6.Matlab混合编程文章:链接 

最近在做一个小工具,需要将.NET中计算的二维数组和交错数组的数据导入到Excel文件中,到博客园找了一下,有不少前辈已经贴了很完善的代码。我稍微改进了下,特意再次分享。由于比较乱,下面代码的出处不知在哪(只知道在博客园),在此谢过。当然先要应用Interop.Excel.

using Excel ;

......

/// <summary> /// 将一维数组转换到Excel文件中: /// 目前支持的数据类型有:DataTable,二维数组,二维交错数组,DataGridView,ArrayList /// </summary> /// <param name="data">数据对象</param> /// <param name="xlsSaveFileName">保存为Excel的文件名</param> /// <returns>是否保存成功</returns> public static bool ConvertDataToExcel<T>(T[] data,string xlsSaveFileName) { //在做这些前,将Excl添加到引用中来!! Excel.Application excel = new Excel.Application(); //如果系统是Excl2007,添加的引用会不一样 //代码如下:Microsoft.Office.Interop.Excel.Application excel //= new Microsoft.Office.Interop.Excel.Application(); if (excel == null) { return false; } else { try { Excel.Workbook xlBook = excel.Workbooks.Add(true); Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1]; //导入数据 for (int i = 0; i <data.Length ; i++) { if (data[i]!=null ) { excel.Cells[1,i + 1] = data[i ].ToString().Trim(); } } xlBook.Saved = true; //保存文件 xlBook.SaveCopyAs(xlsSaveFileName); return true; } catch { return false; } finally { //关闭excel对象,否则会在内存中存在很多excel进程 excel.Quit(); excel = null; GC.Collect(); } } } /// <summary> /// 将二维数组数据导入到Excel中 /// </summary> /// <param name="data">数据</param> /// <param name="xlsSaveFileName">保存的文件名(路径)</param> /// <returns>是否保存成功</returns> public static bool ConvertDataToExcel<T>(T[,] data,string xlsSaveFileName) { //在做这些前,将Excl添加到引用中来!! Excel.Application excel = new Excel.Application(); //如果系统是Excl2007,添加的引用会不一样,代码如下:Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); if (excel == null) { return false; } else { Excel.Workbook xlBook = excel.Workbooks.Add(true); Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1]; int rows = data.GetLength(0);//行 int cols = data.GetLength(1);//列 //导入数据 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (data[i,j]!=null ) { excel.Cells[i + 1, j + 1] = data[i, j].ToString().Trim(); } } } try { xlBook.Saved = true; xlBook.SaveCopyAs(xlsSaveFileName); return true; } catch { return false; } finally { excel.Quit(); excel = null; GC.Collect(); } } } /// <summary> /// 将二维交错数组数组数据导入到Excel中 /// </summary> /// <param name="data">数据</param> /// <param name="xlsSaveFileName">保存的文件名(路径)</param> /// <returns>是否保存成功</returns> public static bool ConvertDataToExcel<T>(T[][] data,string xlsSaveFileName) { //在做这些前,将Excl添加到引用中来!! Excel.Application excel = new Excel.Application(); //如果系统是Excl2007,添加的引用会不一样,代码如下:Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); if (excel == null) { return false; } else { Excel.Workbook xlBook = excel.Workbooks.Add(true); Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1]; int rows = data.GetLength(0);//行 //int cols = data.GetLength(1);//列 //导入数据 for (int i = 0; i < rows; i++) { for (int j = 0; j <data [i ].Length ; j++) { if (data[i][j]!=null ) { excel.Cells[i + 1, j + 1] = data[i][ j].ToString().Trim(); } } } try { xlBook.Saved = true; xlBook.SaveCopyAs(xlsSaveFileName); return true; } catch { return false; } finally { excel.Quit(); excel = null; GC.Collect(); } } }

 

听说有更好的Excel读写组件NPOI,回去试一下看看。。 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2022-01-16
猜你喜欢
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2021-06-15
  • 2021-06-08
  • 2021-07-11
  • 2022-12-23
相关资源
相似解决方案