C#中导出Excel的单元格属性设置.pdf
《C#中导出Excel的单元格属性设置.pdf》由会员分享,可在线阅读,更多相关《C#中导出Excel的单元格属性设置.pdf(13页珍藏版)》请在三一文库上搜索。
1、最近项目中一直在写XML 、Table、Excel 之间的转化。之前一直都是不 考虑格式的导出,今天给出一个格式,让按照格式导出,还真把我这新 手为难了一翻,网上给出的资料基本一样。为了一个单元格文字变色纠 结了很久。下面把学习资料发出,希望对新手学习有所帮助: 下面是会用到的导出属性。 合并单元格属性: worksheet.get_Range(worksheet.CellsrowIndex, columnCount + 1, worksheet.CellsrowIndex + 2, columnCount + 1).MergeCells = true; 设置某一个单元格中字体的颜色: wor
2、ksheet.get_Range(worksheet.CellsrowIndex, 5, worksheet.CellsrowIndex, 8).Font.ColorIndex = 5;(这个在网上 找的一直变不了色,后面自己试出来了) 字体颜色的 index 值: 选定区间设置字符串格式或数字格式: Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(worksheet.CellsrowIndex, 1, worksheet.CellsrowCount+rowIndex-1, columnCount-1); ran
3、ge.NumberFormat = “;/设置数字文本格式 Microsoft.Office.Interop.Excel.Range rangeinfo = worksheet.get_Range(worksheet.CellsrowIndex, 4, worksheet.CellsrowCount + rowIndex - 1, 4); rangeinfo.NumberFormat = “00“ ; 用于汇总和计算时 ( 所计算的字段值必须是数字格式) : worksheet.CellsrowIndex + i, columnCount+1 = “=CEILING(D“ + (rowInde
4、x + 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.Exce
5、l.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.Current
6、Thread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCult ure = new System.Globalization.CultureInfo(“en-US“); Microsoft.Office.Interop.Excel.Workbook workbook = rptExcel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATe mplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Works
7、heet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get _Item(1); worksheet.Name = “报表“;/一个 sheet 的名称 rptExcel.Visible = true;/打开导出的 Excel 文件 worksheet.Cells1, 1 = “27705“;/模版号 rowIndex+; /第二行内容 Microsoft.Office.Interop.Excel.Range rangeinfo1 = worksheet.get_Range(worksheet.Ce
8、llsrowIndex, colindex + 6,worksheet.CellsrowIndex, colindex + 7); rangeinfo1.NumberFormat = “; worksheet.CellsrowIndex, colindex = “S#262229“; worksheet.CellsrowIndex, colindex+6 = dt.Columns13.ColumnName; worksheet.CellsrowIndex, colindex + 7 = dt.Rows013; worksheet.CellsrowIndex, colindex+12 = “EA
9、N“; /合并打印数量单元格 worksheet.CellsrowIndex, columnCount+1 = “打印数 量“; worksheet.CellsrowIndex, columnCount+2 = “包装数 量“; worksheet.get_Range(worksheet.CellsrowIndex, columnCount + 1, worksheet.CellsrowIndex + 2, columnCount + 1).MergeCells = true; worksheet.get_Range(worksheet.CellsrowIndex, columnCount +
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 导出 Excel 单元格 属性 设置
链接地址:https://www.31doc.com/p-5159773.html