C#.NET將數組匯入Excel原始碼

來源:互聯網
上載者:User

最近在做一個小工具,需要將.NET中計算的二維數組和交錯數組的資料匯入到Excel檔案中,到部落格園找了一下,有不少前輩已經貼了很完善的代碼。我稍微改進了下,特意再次分享。由於比較亂,下面代碼的出處不知在哪(只知道在部落格園),在此謝過。當然先要應用Interop.Excel.

using Excel ;

......

Code/// <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,回去試一下看看。。 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.