最近項目中一直在寫XML、Table、Excel之間的轉化。之前一直都是不考慮格式的導出,今天給出一個格式,讓按照格式導出,還真把我這新手為難了一翻,網上給出的資料基本一樣。為了一個單元格文字變色糾結了很久。下面把學習資料發出,希望對新手學習有所幫助:
下面是會用到的導出屬性。
合並單元格屬性:
worksheet.get_Range(worksheet.Cells[rowIndex, columnCount + 1], worksheet.Cells[rowIndex + 2, columnCount + 1]).
MergeCells = true;
設置某一個單元格中字體的顏色:
worksheet.get_Range(worksheet.Cells[rowIndex, 5], worksheet.Cells[rowIndex, 8]).
Font.ColorIndex = 5;(這個在網上找的一直變不了色,后面自己試出來了)
字體顏色的index值:
選定區間設置字符串格式或數字格式:
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(worksheet.Cells[rowIndex, 1], worksheet.Cells[rowCount+rowIndex-1, columnCount-1]);
range.NumberFormat =
"@";//設置數字文本格式
Microsoft.Office.Interop.Excel.Range rangeinfo = worksheet.get_Range(worksheet.Cells[rowIndex, 4], worksheet.Cells[rowCount + rowIndex - 1, 4]);
rangeinfo.NumberFormat =
"00";
用於匯總和計算時(所計算的字段值必須是數字格式):
worksheet.Cells[rowIndex + i, columnCount+1] =
"=CEILING(D" + (rowIndex + i).ToString() + "*1.01+1,2)"; i是變量
PS:一下代碼則可導出如下圖的Excel格式:
// 導出為Excel格式文件 /// </summary> /// <param name="dt">作為數據源的DataTable</param> /// <param name="saveFile">帶路徑的保存文件名</param> /// <param name="title">一個Excel sheet的標題</param> public static void DataTabletoExcel(System.Data.DataTable dt, string saveFile) { Microsoft.Office.Interop.Excel.Application rptExcel = new Microsoft.Office.Interop.Excel.Application(); if (rptExcel == null) { PublicClass.HintBox("無法打開EXcel,請檢查Excel是否可用或者是否安裝好Excel"); return; } int rowCount = dt.Rows.Count;//行數 int columnCount = dt.Columns.Count;//列數 int rowIndex = 1; int colindex = 1; //保存文化環境 System.Globalization.CultureInfo currentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Microsoft.Office.Interop.Excel.Workbook workbook = rptExcel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1); worksheet.Name = "報表";//一個sheet的名稱 rptExcel.Visible = true;//打開導出的Excel文件 worksheet.Cells[1, 1] = "27705";//模版號 rowIndex++; //第二行內容 Microsoft.Office.Interop.Excel.Range rangeinfo1 = worksheet.get_Range(worksheet.Cells[rowIndex, colindex + 6],worksheet.Cells[rowIndex, colindex + 7]); rangeinfo1.NumberFormat = "@"; worksheet.Cells[rowIndex, colindex] = "S#262229"; worksheet.Cells[rowIndex, colindex+6] = dt.Columns[13].ColumnName; worksheet.Cells[rowIndex, colindex + 7] = dt.Rows[0][13]; worksheet.Cells[rowIndex, colindex+12] = "EAN"; //合並打印數量單元格 worksheet.Cells[rowIndex, columnCount+1] = "打印數量"; worksheet.Cells[rowIndex, columnCount+2] = "包裝數量"; worksheet.get_Range(worksheet.Cells[rowIndex, columnCount + 1], worksheet.Cells[rowIndex + 2, columnCount + 1]).MergeCells = true; worksheet.get_Range(worksheet.Cells[rowIndex, columnCount + 2], worksheet.Cells[rowIndex + 2, columnCount + 2]).MergeCells = true; rowIndex++; //第三行內容 worksheet.Cells[rowIndex, 1] = "貨名"; worksheet.Cells[rowIndex, 2] = "Line"; //合並第三行第二列 worksheet.get_Range(worksheet.Cells[rowIndex, 2], worksheet.Cells[rowIndex+1, 2]).MergeCells = true; worksheet.Cells[rowIndex, 3] = "序號"; worksheet.Cells[rowIndex, 4] = "數量"; worksheet.Cells[rowIndex, 5] = "1 (EUR) size"; worksheet.Cells[rowIndex, 6] = "1a (€)"; worksheet.Cells[rowIndex, 7] = "2 (UK) size"; worksheet.Cells[rowIndex, 8] = "2a (₤)"; worksheet.get_Range(worksheet.Cells[rowIndex, 5], worksheet.Cells[rowIndex, 8]).Font.Bold = true; worksheet.Cells[rowIndex, 9] = "4"; worksheet.Cells[rowIndex, 10] = "5"; worksheet.Cells[rowIndex, 11] = "6"; worksheet.Cells[rowIndex, 12] = "7"; worksheet.Cells[rowIndex, 13] = "8(barcode)"; worksheet.Cells[rowIndex, 14] = "9"; worksheet.get_Range(worksheet.Cells[rowIndex, 5], worksheet.Cells[rowIndex, 8]).Font.ColorIndex = 5; rowIndex++; //填充列標題 for (int i = 0; i < columnCount-1; i++) { if (i > 0) { worksheet.Cells[rowIndex, i + 2] = dt.Columns[i].ColumnName; } else { worksheet.Cells[rowIndex, i+1] = dt.Columns[i].ColumnName; } } rowIndex++; //創建對象數組存儲DataTable的數據,這樣的效率比直接將Datateble的數據填充worksheet.Cells[row,col]高 object[,] objData = new object[rowCount, columnCount]; //填充內容到對象數組 for (int r = 0; r < rowCount; r++) { for (int col = 0; col < columnCount-1; col++) { objData[r, col] = dt.Rows[r][col].ToString(); } System.Windows.Forms.Application.DoEvents(); } //將對象數組的值賦給Excel對象 Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(worksheet.Cells[rowIndex, 1], worksheet.Cells[rowCount+rowIndex-1, columnCount-1]); range.NumberFormat = "@";//設置數字文本格式 Microsoft.Office.Interop.Excel.Range rangeinfo = worksheet.get_Range(worksheet.Cells[rowIndex, 4], worksheet.Cells[rowCount + rowIndex - 1, 4]); rangeinfo.NumberFormat = "00"; range.Value2 = objData; for (int i = 0; i < rowCount; i++) { if (i > 0) { //計算打印數量 worksheet.Cells[rowIndex + i, columnCount+1] = "=CEILING(D" + (rowIndex + i).ToString() + "*1.01+1,2)"; } else { worksheet.Cells[rowIndex + i, columnCount+1] = "=CEILING(D" + (rowIndex + i).ToString() + "*1.01+1,2)"; } } //設置格式 rptExcel.StandardFont = "新細明體"; rptExcel.StandardFontSize = 12; worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowCount+rowIndex, columnCount]).Columns.AutoFit();//設置單元格寬度為自適應 worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowCount+rowIndex, columnCount]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//居中對齊 //worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]).Font.Bold = true; //worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]).Font.Color= ConsoleColor.Blue; // worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[rowCount + 2, columnCount]).Borders.LineStyle = 1;//設置邊框 //匯總 rowIndex = rowCount + rowIndex; worksheet.Cells[rowIndex, 4] = "=SUM(D5:D10)"; //恢復文化環境 System.Threading.Thread.CurrentThread.CurrentCulture = currentCI; try { //rptExcel.Save(saveFile); //自動創建一個新的Excel文檔保存在“我的文檔”里,如果不用SaveFileDialog就可用這種方法 workbook.Saved=true; workbook.SaveCopyAs(saveFile);//以復制的形式保存在已有的文檔里 PublicClass.HintBox("數據已經成功導出為Excel文件!"); } catch (Exception ex) { PublicClass.HintBox("導出文件出錯,文件可能正被打開,具體原因:" + ex.Message); } finally { dt.Dispose(); rptExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(rptExcel); System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); GC.Collect(); KillAllExcel(); } } /// <summary> /// 獲得所有的Excel進程 /// </summary> /// <returns>所有的Excel進程</returns> private static List<Process> GetExcelProcesses() { Process[] processes = Process.GetProcesses(); List<Process> excelProcesses = new List<Process>(); for (int i = 0; i < processes.Length; i++) { if (processes[i].ProcessName.ToUpper() == "EXCEL") excelProcesses.Add(processes[i]); } return excelProcesses; } private static void KillAllExcel() { List<Process> excelProcess = GetExcelProcesses(); for (int i = 0; i < excelProcess.Count; i++) { excelProcess[i].Kill(); } }
