c# ExportToExcels
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Business;using System.Runtime.InteropServices;using System.Data.OleDb;using Microsoft.Office.Interop.Excel;/// <summary>/// ExportToExcels 的摘要說明/// </summary>public class ExportToExcels{ public ExportToExcels() { // // TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 導出到 Excel 文件 /// </summary> /// <param name="rs">當前記錄</param> /// <param name="flds">要導出的字段</param> /// <param name="fn">文件的路徑文件名</param> /// <param name="dcfg">詞典配置工具</param> public static void ExportToExcel(DataAccess.Record[] rs, DataAccess.Field[] flds, string fn, DictionaryConfigure dcfg) { Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); // 建立Excel對象 excel.Application.Workbooks.Add(true); // Excel表為添加狀態 //for (int i = 0; i < flds.Length; i++) // 填充表頭 //{ // excel.Cells[1, i + 1] = flds[i].Name; //} excel.Columns.EntireColumn.AutoFit();//列寬自適應。 excel.Cells[1, 1] = "2012年省級政府投資項目儲備庫項目申報匯總表"; Microsoft.Office.Interop.Excel.Range r; r = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 19]); //取得合并的區域 r.MergeCells = true; // 設置整個報表的標題格式 //excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true; excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 18; // 設置整個報表的標題為跨列居中 excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Select(); excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenter; excel = setExcel(excel, "填報單位", 2, 1, 3, 1, false, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目類別", 2, 2, 3, 2, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目名稱", 2, 3, 3, 3, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "建設性質", 2, 4, 3, 4, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "建設起止年限", 2, 5, 3, 5, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "建設結束年限", 2, 6, 3, 6, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目建設單位", 2, 7, 3, 7, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "建設規模及主要內容", 2, 8, 3, 8, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "總投資(萬元)", 2, 9, 3, 9, true, 58, 20, XlHAlign.xlHAlignCenter); //excel = setExcel(excel, "擬申請省級財政性資金", 2, 10, 2, 13, true, 58, 20, XlHAlign.xlHAlignCenter); //excel = setExcel(excel, "共計(萬元)", 3, 10, 3, 10, false, 58, 20, XlHAlign.xlHAlignCenter); //excel = setExcel(excel, "申請年度", 3, 11, 3, 11, false, 58, 20, XlHAlign.xlHAlignCenter); //excel = setExcel(excel, "擬申請省級財政性專項資金名稱", 3, 12, 3, 12, false, 58, 20, XlHAlign.xlHAlignCenter); //excel = setExcel(excel, "擬申請金額(萬元)", 3, 13, 3, 13, false, 58, 20, XlHAlign.xlHAlignCenter); //excel = setExcel(excel, "擬申請省級財政性資金", 2, 10, 2, 13, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "共計(萬元)", 2, 10, 3, 10, false, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "申請年度", 2, 11, 3, 11, false, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "擬申請省級財政性專項資金名稱", 2, 12, 3, 12, false, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "擬申請金額(萬元)", 2, 13, 3, 13, false, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目審批文號", 2, 14, 3, 14, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目進展情況", 2, 15, 3, 15, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目用地情況(畝)", 2, 16, 3, 16, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "項目建設必要性", 2, 17, 3, 17, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "產業政策、城鄉規劃、土地利用規劃符合性", 2, 18, 3, 18, true, 58, 20, XlHAlign.xlHAlignCenter); excel = setExcel(excel, "處理狀態", 2, 19, 3, 19, true, 58, 20, XlHAlign.xlHAlignCenter); for (int i = 0; i < rs.Length; i++) // 填充數據 { for (int j = 0; j < flds.Length; j++) { string txt = rs[i].getStringValue(flds[j].ID); if (!flds[j].DictionaryID.Equals("")) { txt = Business.DictionaryConfigure.GetCatalogNameByDictionary(txt, dcfg.getDictionary(flds[j].DictionaryID)); } if (flds[j].EditIsDate) { txt = (txt.Length > 8 ? txt.Substring(0, 8) : txt); txt = Utility.StringUtility.Alt(txt, "????-??-??", "?"); } excel.Cells[i + 3, j + 1] = txt; } } excel.Visible = false; excel.DisplayAlerts = false; excel.ActiveWorkbook.SaveAs(fn, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); KillExcelProcess(excel); } public static Microsoft.Office.Interop.Excel.ApplicationClass setExcel(Microsoft.Office.Interop.Excel.ApplicationClass excel, string title, int xNum, int yNum, int xNum2, int yNum2,bool isMerge, int height, int width, XlHAlign xl) { excel.Cells[xNum, yNum] = title; Microsoft.Office.Interop.Excel.Range r; r = excel.get_Range(excel.Cells[xNum, yNum], excel.Cells[xNum2, yNum2]); //r.MergeCells = true; r.Merge(isMerge); //r.ColumnWidth = width; //r.RowHeight = height; r.HorizontalAlignment = xl; r.WrapText = true; return excel; } /// <summary> /// 獲取進程標識 /// </summary> /// <param name="hwnd">輸入參數:句柄</param> /// <param name="ID">輸出參數:進程標識</param> /// <returns></returns> [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); /// <summary> /// 關閉 Excel 進程 /// </summary> /// <param name="excel"></param> private static void KillExcelProcess(Microsoft.Office.Interop.Excel.ApplicationClass excel) { //------------ 方法 1 ----------------- //excel.Quit(); //excel = null; //GC.Collect(); // 垃圾回收 //------------ 方法 2 ----------------- try { IntPtr handler = new IntPtr(excel.Hwnd); // 句柄 int processid = 0; // 進程標識 GetWindowThreadProcessId(handler, out processid); System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(processid); // 進程 p.Kill(); // 殺除進程 } catch { excel.Workbooks.Close(); excel.Quit(); } //----------------------------- } /// <summary> /// 從指定的Excel文件導入 /// </summary> /// <param name="strFileName">導入文件</param> /// <returns></returns> public DataSet importFromExcel(string strFileName) { if (strFileName == "") return null; string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + strFileName + ";Extended Properties=Excel 8.0"; OleDbDataAdapter excelDA = new OleDbDataAdapter("select * from [Sheet1$]", strConn); // 連接字符串 DataSet ds = new DataSet(); // 建立數據集,用于存放導入Excel的數據 excelDA.Fill(ds, "ExcelInfo"); // 填充DataSet return ds; } /// <summary> /// 將記錄集導出到 Excel 文件 /// </summary> /// <param name="rs">當前記錄集</param> /// <param name="flds">要導出的字段</param> /// <param name="fn">文件的路徑文件名</param> public static void RecordsToExcel(DataAccess.Record[] rs, DataAccess.Field[] flds, string fn) { Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); // 建立Excel對象 excel.Application.Workbooks.Add(true); // Excel表為添加狀態 for (int i = 0; i < flds.Length; i++) // 填充表頭 { excel.Cells[1, i + 1] = flds[i].Name; } for (int i = 0; i < rs.Length; i++) // 填充數據 { for (int j = 0; j < flds.Length; j++) { excel.Cells[i + 2, j + 1] = rs[i].getStringValue(flds[j].ID); } } excel.Visible = false; excel.DisplayAlerts = false; excel.ActiveWorkbook.SaveAs(fn, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); KillExcelProcess(excel); }}
總結
以上是生活随笔為你收集整理的c# ExportToExcels的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jackson序列化和反序列化
- 下一篇: MySQL索引设计原则