Excel格式控制代码样例
2012-07-19 12:57
197 查看
提示:此份代码我提供了TXT格式下载!
微软的Excel对象模型包括了128个不同的对象,从矩形,文本框等简单的对象到透视表,图表等复杂的对象.下面我们简单介绍一下其中最重要的四个对象。
(1) Application对象。Application对象处于Excel对象层次结构的顶层,表示Excel自身的运行环境。
(2) Workbook对象。Workbook对象直接地处于Application对象的下层,表示一个Excel工作薄文件。
(3) Worksheet对象。Worksheet对象包含于Workbook对象,表示一个Excel工作表。
(4) Range对象。Range对象包含于Worksheet对象,表示Excel工作表中的一个或多个单元格。
C#操作代码样列:
object miss = Missing.Value;//缺省变量
Excel.Application excel = new Excel.Application ();//引用Excel对象
excel.Application.Workbooks.Add ( true );//引用Excel工作簿
excel.Visible = true ;//使Excel可视
excelApp.Caption = "测试报表";
workSheet.Name = "学生信息表";
workSheet.Columns.ColumnWidth = 20;//全局行宽
workSheet.Columns.RowHeight = 20;//全局列高
workSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;//纸张大小
workSheet.PageSetup.PrintTitleRows = "$1:$3";//顶端标题行
workSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;//页面方向为横向
workSheet.PageSetup.TopMargin = excelApp.CentimetersToPoints(2);//上边距为2厘米(厘米转像素)
workSheet.PageSetup.BottomMargin = excelApp.CentimetersToPoints(2);//下边距为2厘米(厘米转像素)
workSheet.PageSetup.LeftMargin = excelApp.CentimetersToPoints(1.5);//左边距为1.5厘米(厘米转像素)
workSheet.PageSetup.RightMargin = excelApp.CentimetersToPoints(1.5);//右边距为1.5厘米(厘米转像素)
workSheet.PageSetup.HeaderMargin = excelApp.CentimetersToPoints(1.2);//页眉边距为1.2厘米(厘米转像素)
workSheet.PageSetup.FooterMargin = excelApp.CentimetersToPoints(1);//页脚边距为1厘米(厘米转像素)
workSheet.PageSetup.CenterHorizontally = true;//页面水平居中
workSheet.PageSetup.CenterVertically = false;//页面不垂直居中
workSheet.PageSetup.CenterFooter = "第&P页,共&N页";//中间页脚内容
range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//设置水平对齐方式
range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式
workSheet.get_Range(workSheet.Cells[1, colIndex], workSheet.Cells[1, colIndex])//选择操作块
Range range1 = workSheet.get_Range("A1", "E20");//选择操作块
range1.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);//设置边框颜色
range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).LineStyle = XlLineStyle.xlContinuous;//斜杠
range1.Borders.get_Item(XlBordersIndex.xlDiagonalUp).LineStyle = XlLineStyle.xlContinuous;//反斜杠
range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).Color = System.Drawing.ColorTranslator.ToOle(Color.Gold);
range1.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;//块内竖线
range1.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;//块内横线
//统一设置边框风格
range1.BorderAround(XlLineStyle.xlDouble, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
range1.Font.Bold = true;//设置黑体
range1.Font.Size = 18;//设置字体大小
range1.Font.Name = "仿宋";//设置字体
range1.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//设置字体颜色
excelApp.Application.DisplayAlerts = false;//使合并操作不提示警告信息
range1.Merge(true);//参数为True则为每一行合并为一个单元格 */
Range range1 = (Range)workSheet.Cells[2, 2];//选择操作块
range1.NumberFormatLocal = "@"; //设置格式为文本
range1.Cells.ColumnWidth = 11; //单元格列高
range1.Rows.ColumnWidth = 15;//单元格行宽
workSheet.Cells[ 3, 1] = "成绩总结"; //写入数据
((Excel.Range)workSheet.Rows[ 3, Type.Missing]).PageBreak = (int)Excel.XlPageBreak.xlPageBreakManual;//设置分页线
workSheet.PageSetup.PrintArea="A1:O12,C12:B12";//设置打印区域
workBook.RefreshAll();//更新所有工作表
workBook.SaveAs("C:\\Users\\Administrator\\Desktop\\test.xls", miss, miss, miss, miss, miss, XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); //保存WorkBook
workBook = null;//消除对象
excelApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出
excelApp = null;
GC.Collect();//强制对所有代进行垃圾回收。
微软的Excel对象模型包括了128个不同的对象,从矩形,文本框等简单的对象到透视表,图表等复杂的对象.下面我们简单介绍一下其中最重要的四个对象。
(1) Application对象。Application对象处于Excel对象层次结构的顶层,表示Excel自身的运行环境。
(2) Workbook对象。Workbook对象直接地处于Application对象的下层,表示一个Excel工作薄文件。
(3) Worksheet对象。Worksheet对象包含于Workbook对象,表示一个Excel工作表。
(4) Range对象。Range对象包含于Worksheet对象,表示Excel工作表中的一个或多个单元格。
C#操作代码样列:
object miss = Missing.Value;//缺省变量
Excel.Application excel = new Excel.Application ();//引用Excel对象
excel.Application.Workbooks.Add ( true );//引用Excel工作簿
excel.Visible = true ;//使Excel可视
excelApp.Caption = "测试报表";
workSheet.Name = "学生信息表";
workSheet.Columns.ColumnWidth = 20;//全局行宽
workSheet.Columns.RowHeight = 20;//全局列高
workSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;//纸张大小
workSheet.PageSetup.PrintTitleRows = "$1:$3";//顶端标题行
workSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;//页面方向为横向
workSheet.PageSetup.TopMargin = excelApp.CentimetersToPoints(2);//上边距为2厘米(厘米转像素)
workSheet.PageSetup.BottomMargin = excelApp.CentimetersToPoints(2);//下边距为2厘米(厘米转像素)
workSheet.PageSetup.LeftMargin = excelApp.CentimetersToPoints(1.5);//左边距为1.5厘米(厘米转像素)
workSheet.PageSetup.RightMargin = excelApp.CentimetersToPoints(1.5);//右边距为1.5厘米(厘米转像素)
workSheet.PageSetup.HeaderMargin = excelApp.CentimetersToPoints(1.2);//页眉边距为1.2厘米(厘米转像素)
workSheet.PageSetup.FooterMargin = excelApp.CentimetersToPoints(1);//页脚边距为1厘米(厘米转像素)
workSheet.PageSetup.CenterHorizontally = true;//页面水平居中
workSheet.PageSetup.CenterVertically = false;//页面不垂直居中
workSheet.PageSetup.CenterFooter = "第&P页,共&N页";//中间页脚内容
range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//设置水平对齐方式
range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式
workSheet.get_Range(workSheet.Cells[1, colIndex], workSheet.Cells[1, colIndex])//选择操作块
Range range1 = workSheet.get_Range("A1", "E20");//选择操作块
range1.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);//设置边框颜色
range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).LineStyle = XlLineStyle.xlContinuous;//斜杠
range1.Borders.get_Item(XlBordersIndex.xlDiagonalUp).LineStyle = XlLineStyle.xlContinuous;//反斜杠
range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).Color = System.Drawing.ColorTranslator.ToOle(Color.Gold);
range1.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;//块内竖线
range1.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;//块内横线
//统一设置边框风格
range1.BorderAround(XlLineStyle.xlDouble, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
range1.Font.Bold = true;//设置黑体
range1.Font.Size = 18;//设置字体大小
range1.Font.Name = "仿宋";//设置字体
range1.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//设置字体颜色
excelApp.Application.DisplayAlerts = false;//使合并操作不提示警告信息
range1.Merge(true);//参数为True则为每一行合并为一个单元格 */
Range range1 = (Range)workSheet.Cells[2, 2];//选择操作块
range1.NumberFormatLocal = "@"; //设置格式为文本
range1.Cells.ColumnWidth = 11; //单元格列高
range1.Rows.ColumnWidth = 15;//单元格行宽
workSheet.Cells[ 3, 1] = "成绩总结"; //写入数据
((Excel.Range)workSheet.Rows[ 3, Type.Missing]).PageBreak = (int)Excel.XlPageBreak.xlPageBreakManual;//设置分页线
workSheet.PageSetup.PrintArea="A1:O12,C12:B12";//设置打印区域
workBook.RefreshAll();//更新所有工作表
workBook.SaveAs("C:\\Users\\Administrator\\Desktop\\test.xls", miss, miss, miss, miss, miss, XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); //保存WorkBook
workBook = null;//消除对象
excelApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出
excelApp = null;
GC.Collect();//强制对所有代进行垃圾回收。
相关文章推荐
- java 使用poi导出excel,可控制固定前2列固定标头排版,带统计数据格式的
- excel列(單元格)格式的控制????
- C# 导出Excel (可控制格式)
- 假导出Excel功能实现,按CSV格式快速导出功能代码参考(追求代码追求简洁复用)浙江杭州
- ASP.NET控制Word/EXCEL转换成PDF格式
- java web将数据导出为Excel格式文件代码片段
- iOS自动布局(二)--代码控制--Visual Format language(可视化格式语言)
- c#导入导出excel(无格式控制)
- web导出excel格式控制【转】
- RC5编码格式的遥控器解码、PCA9633器件控制代码示例、串口通信程序示例、IIC通信示例
- 文档控制格式导excel
- 从单向链表中删除指定值的节点(格式控制真坑,末尾要打印空格,样例输出有错误)
- 假导出Excel功能实现,按CSV格式快速导出功能代码参考(追求代码追求简洁复用)浙江杭州
- c#实现word,excel转pdf代码及部分Office 2007文件格式转换为xps和pdf代码整理
- python xlwt写excel格式控制
- 网页中点击链接后控制格式变化的代码——Dreamweaver
- vb代码控制 Excel锁定单元格
- C#控制Excel格式的方法
- excel导出格式 控制
- 最简单DataSet导出EXCEL并进行格式控制等操作