1、引用 MicroSoft.Office.Interop.Excel 在
C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
及Office
C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL
2、初始信息
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); //初始化表信息 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); fileCounter = 1; if (xlApp == null) { return; } xlApp.Visible = true;
最后
xlApp.ActiveWorkbook.Close(true, AppDomain.CurrentDomain.BaseDirectory + "檢查結果"); xlApp.Quit();
3、一些好用的方法
(1)處理sheet頁
/// <summary> /// 增加Sheet頁,調整sheet名 /// </summary> /// <param name="xlApp"></param> private void AddSheetAndRenameSheet(Microsoft.Office.Interop.Excel.Application xlApp) { Workbooks workbooks = xlApp.Workbooks; Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet); //如果不去刻意設置表順序,打開后的Excel只有個Sheet1,這時取1號就是當前的Sheet,再增加Sheet后,會在Sheet1的前面插入。
Worksheet worksheet4 = (Worksheet)workbook.Worksheets.get_Item(1); worksheet4.Name = "第四個表單"; PrintHeader(worksheet4, GetModelTable()); Worksheet worksheet3 = workbook.Worksheets.Add(); worksheet3.Name = "第三個表單"; PrintHeader(worksheet3, GetModelTable()); Worksheet worksheet2 = workbook.Worksheets.Add(); worksheet2.Name = "第二個表單"; PrintHeader(worksheet2, GetModelTable2()); Worksheet worksheet1 = workbook.Worksheets.Add(); worksheet1.Name = "第一個表單"; PrintHeader(worksheet1, GetModelTable3()); }
(2)
凍結表頭
/// <summary> /// 凍結表頭 /// </summary> /// <param name="xlApp"></param> private void FrozenHeader(Microsoft.Office.Interop.Excel.Application xlApp) { foreach (Worksheet workSheet in xlApp.ActiveWorkbook.Worksheets) { workSheet.Activate(); xlApp.ActiveWindow.SplitRow = workSheet.UsedRange.CurrentRegion.Rows.Count;//當前已經用到的區域,通常只凍結第一行 //xlApp.ActiveWindow.SplitColumn = workSheet.UsedRange.CurrentRegion.Columns.Count;//當前已經用到的區域,凍結列。 不常用
xlApp.ActiveWindow.FreezePanes = true;
} }
(3)輸出表頭
/// <summary> /// 打印表頭 /// </summary> private void PrintHeader(Worksheet worksheet1, System.Data.DataTable dtInput) { Range range1; //表頭 for (int i = 0; i < dtInput.Columns.Count; i++) { worksheet1.Cells[1, i + 1] = dtInput.Columns[i].ColumnName; range1 = (Range)worksheet1.Cells[1, i + 1]; range1.Interior.ColorIndex = 15; range1.Font.Bold = true; range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//水平居中 range1.ColumnWidth = 15; } }
(4)將內容寫入到Excel中
/// <summary> /// 將指定的內容寫入到Excel中 /// </summary> /// <param name="xlApp"></param> /// <param name="SheetIndex">從多少行開始寫第一個表內數據</param> /// <param name="startIndex"></param> /// <param name="dt"></param> private int PrintContent(Microsoft.Office.Interop.Excel.Application xlApp, int SheetIndex, int startIndex, System.Data.DataTable dtInput) { Workbooks workbooks = xlApp.Workbooks; Workbook workbook = workbooks[1]; Worksheet worksheet = workbook.Worksheets[SheetIndex]; worksheet.Activate(); //表內容 for (int i = 0; i < dtInput.Rows.Count; i++) { ((Range)worksheet.Cells[startIndex + i + 2, 1]).Activate();//視角跟隨 for (int j = 0; j < dtInput.Columns.Count; j++) { worksheet.Cells[startIndex + i + 1, j + 1] = dtInput.Rows[i][j].ToString(); } } startIndex += dtInput.Rows.Count; return startIndex; }
(5)將數據按照表寫入Excel(批量寫)
/// <summary> /// 將指定的內容寫入到Excel中 /// </summary> private int PrintContentWithBulk(Microsoft.Office.Interop.Excel.Application xlApp, int sheetIndex, int startIndex, System.Data.DataTable dtInput) { Workbooks workbooks = xlApp.Workbooks; Workbook workbook = workbooks[1]; Worksheet worksheet = workbook.Worksheets[sheetIndex]; worksheet.Activate(); //在內存中構造一個數據塊 object[,] objData = new Object[dtInput.Rows.Count, dtInput.Columns.Count]; for (int i = 0; i < dtInput.Rows.Count; i++) for (int j = 0; j < dtInput.Columns.Count; j++) objData[i, j] = dtInput.Rows[i][j]; //選中Excel中相同大小的一塊數據 Range r = worksheet.get_Range("A" + (1 + startIndex).ToString(), Missing.Value); r = r.get_Resize(dtInput.Rows.Count, dtInput.Columns.Count); r.Value = objData; startIndex += dtInput.Rows.Count; return startIndex; }
(7)如果要填寫的記錄數可能大於Excel能接受的數目
int fileCounter; /// <summary> /// 寫數據 /// </summary> void WriteContent() { System.Data.DataTable dtAlls = GetData(); System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); //初始化表信息 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); fileCounter = 1; LineCounter = 0; if (xlApp == null) { return; } xlApp.Visible = true; AddSheetAndRenameSheet(xlApp); for (int i = 0; i < dtAlls.Count; i++) { if (LineCounter > 65000)//這里設置條目數接近Excel行數允許的最大值時,就創建第二個文檔 { AddBorder(xlApp); xlApp.ActiveWorkbook.Close(true, AppDomain.CurrentDomain.BaseDirectory + "XXX數據結果" + "(" + fileCounter.ToString() + ")"); xlApp.Quit(); fileCounter++; xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { return; } xlApp.Visible = true; AddSheetAndRenameSheet(xlApp); LineCounter = 1; } else {//寫入這個表的數據 lineCounter=PrintContent(xlApp, lineCounter,dt); } } AddBorder(xlApp); if (fileCounter == 1) xlApp.ActiveWorkbook.Close(true, AppDomain.CurrentDomain.BaseDirectory + "XXX數據結果"); else xlApp.ActiveWorkbook.Close(true, AppDomain.CurrentDomain.BaseDirectory + "XXX數據結果" + "(" + fileCounter.ToString() + ")"); xlApp.Quit(); }
(8)視角跟隨
((Range)worksheet.Cells[startIndex + i + 2, 1]).Activate();//視角跟隨
(9)加邊框
/// <summary> /// 給所有的表格加邊框 /// </summary> /// <param name="xlApp"></param> private void AddBorder(Microsoft.Office.Interop.Excel.Application xlApp) { foreach (Worksheet workSheet in xlApp.ActiveWorkbook.Worksheets) { int RowCount = workSheet.UsedRange.CurrentRegion.Rows.Count; int ColumnCount = workSheet.UsedRange.CurrentRegion.Columns.Count; Range range = workSheet.Range[workSheet.Cells[1, 1], workSheet.Cells[RowCount, ColumnCount]];//設置邊框 range.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic); } }