最近项目中一直在写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格式文件 /// ///
作为数据源的DataTable ///
带路径的保存文件名 ///
一个 Excel sheet的标题 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 cur...