.Net的Excel 导出 格式设置

添加引用:Microsoft   Excel   11.0   Object   Library ;

添加:using Microsoft.Office.Interop.Excel;

一、打开Excel文件============================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Open(@"E:\aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

excel1.Visible = true;

二、新建Excel对象============================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(XlWBATemplate.xlWBATWorksheet或true);

worksheet1.Activate();//激活sheet1表

excel1.Visible = true;

三、新建Excel表============================

  Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

Worksheet worksheet1 =(Worksheet)workbook1.Worksheets.Add(Type.Missing,workbook1.Worksheets[1], 1, Type.Missing);

excel1.Visible = true;

四、重命名Excel表名============================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"或1];

worksheet1.Name = "工作计划表";

excel1.Visible = true;

五、设置或修改Excel表单元格内容========================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

worksheet1.Cells[1, 1] = "姓名";

worksheet1.Cells[1, 2] = "性别";

excel1.Visible = true;

六、设置Excel表行宽和列高===========================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

worksheet1.Columns.ColumnWidth = 20;//全局行宽

worksheet1.Columns.RowHeight = 20;//全局列高

Range range1 = (Range) worksheet1.Cells[2, 1];

range1.Columns.ColumnWidth = 40;//单元格行宽

range1.Columns.RowHeight = 40;//单元格列高

excel1.Visible = true;

七、设置Excel表单元格边框===========================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

Range range1 = (Range)worksheet1.Cells[2, 2];

range1.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);

range1.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;

range1.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;

range1.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;

range1.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;

//也可用后面的代码代替上面四项range1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic,null);

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);

excel1.Visible = true;

八、Excel表块操作============================

  Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

Range range1 = worksheet1.get_Range("A2", "E8");//选择操作块

range1.Font.Bold = true;//设置黑体

range1.Font.Size = 18;//设置字体大小

  range1.Font.Name = "仿宋";//设置字体

range1.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//设置字体颜色

range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//设置水平对齐方式

range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式

range1.Value2 = "123\r\n456";

range1.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;

range1.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;

range1.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;

range1.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;

//也可用后面的代码代替上面四项range1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic,null);

range1.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;//块内竖线

range1.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;//块内横线

excel1.Visible = true;

九、Excel表单元格合并============================

  Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

Range range1 = worksheet1.get_Range("A2", "E8");//选择操作块

range1.Value2 = "123\r\n456";

excel1.Application.DisplayAlerts = false;//使合并操作不提示警告信息

range1.Merge(false);//参数为True则为每一行合并为一个单元格

excel1.Application.DisplayAlerts = true;

excel1.Visible = true;

十、复制Excel表============================

Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

worksheet1.Cells[1, 1] = "123";

worksheet1.Copy(Type.Missing, worksheet1);

Worksheet worksheet2 =(Worksheet)worksheet1.Next;

//worksheet2.Name = "Sheet2";

excel1.Visible = true;

十一、页面设置============================

 Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();

Workbook workbook1 = excel1.Workbooks.Add(true);

excel1.Caption = "我的报表";

Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];

  worksheet1.PageSetup.PaperSize = XlPaperSize.xlPaperA3;//纸张大小

 worksheet1.PageSetup.PrintTitleRows = "$1:$3";//顶端标题行

worksheet1.PageSetup.Orientation = XlPageOrientation.xlLandscape;//页面方向为横向

worksheet1.PageSetup.TopMargin = excel1.CentimetersToPoints(2);//上边距为2厘米(厘米转像素)

worksheet1.PageSetup.BottomMargin = excel1.CentimetersToPoints(2);//下边距为2厘米(厘米转像素)

worksheet1.PageSetup.LeftMargin = excel1.CentimetersToPoints(1.5);//左边距为1.5厘米(厘米转像素)

worksheet1.PageSetup.RightMargin = excel1.CentimetersToPoints(1.5);//右边距为1.5厘米(厘米转像素)

worksheet1.PageSetup.HeaderMargin = excel1.CentimetersToPoints(1.2);//页眉边距为1.2厘米(厘米转像素)

worksheet1.PageSetup.FooterMargin = excel1.CentimetersToPoints(1);//页脚边距为1厘米(厘米转像素)

worksheet1.PageSetup.CenterHorizontally = true;//页面水平居中

worksheet1.PageSetup.CenterVertically = false;//页面不垂直居中

worksheet1.PageSetup.CenterFooter = "第&P页,共&N页";//中间页脚内容

excel1.Visible = true;

时间: 2024-10-13 01:22:19

.Net的Excel 导出 格式设置的相关文章

Excel导出时设置单元格的格式为文本

问题: 用excel导出数据时,如何设置单元格格式的数字分类为"文本",默认是"常规"? 比如:导出编码0235A089,在Excel查看默认显示的是没有前面的0. 解决方法: 用设置单元格的 NumberFormatLocal 属性即可: xlapp.Selection.NumberFormatLocal = "@" Excel导出时设置单元格的格式为文本,布布扣,bubuko.com

POI Excel导出样式设置

HSSFSheet sheet = workbook.createSheet("sheetName"); //创建sheetsheet.setVerticallyCenter(true); //下面样式可作为导出左右分栏的表格模板sheet.setColumnWidth((short) 0, (short) 2600);// 设置列宽sheet.setColumnWidth((short) 1, (short) 2400);sheet.setColumnWidth((short) 2,

goalng导出excel(csv格式)

最近项目中有个小需求,需要将查询结果导出到excel.之间前java比较容易,使用POI很容易就能实现,查了下golang的文档,发现golang下边并没有导出excel的包,但是却有一个encoding/csv的包,看了下发现可以导出csv文件,大家都知道csv文件其实就是文本格式的excel文件,可以直接通过excel打开或是导入excel. 看起来挺好的,问题如愿解决,但是事实证明对已一个还不成熟的语言或是库最好还是先测一下的好.兴冲冲的卸了测试例子,成功导出了一个text.csv文件,一

MyXls导出Excel的各种设置

MyXls是一个操作Excel的开源类库,支持设置字体.列宽.行高(由BOSSMA实现).合并单元格.边框.背景颜色.数据类型.自动换行.对齐方式等,通过众多项目的使用表现,证明MyXls对于创建简单格式的Excel文件十分快捷方便. 本文将通过实例的方式详细说明如何通过各种属性设置MyXls的样式,并附带示例程序的源代码. // 准备测试数据 List<PersonInfo> list = new List<PersonInfo>(); for (int i = 1; i <

关于project导出excel的格式问题

1.如出现提示格式问题的时候,这样设置: 在"工具"→"选项"→"安全性"中,在"旧式格式"下选择"允许加载使用旧式文件格式或非默认文件格式的文件(A)"单选框.保存后重复步骤(1),即可导出Excel表. 2.关于project任务层级导出excel的问题 1.制作好Project文件. 2.最前面插入一列:大纲级别. 3.新建Excel文件,Copy Project所有内容到Excel工作表中. 4.在

C#将网页数据导出Excel时编码设置

1 public void DGToExcel() 2 { 3 Response.ClearContent(); 4 Response.Charset = "GB2312";//内容编码 5 Response.ContentType = "application/ms-excel"; 6 Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//内容编码 7 R

Birt设置导出格式和去掉多余按钮的方法

1.设置导出格式: webcontent>birt>pages>dialog>ExportReportDialogFragment.jsp页面: 找到for ( int i = 0; i < supportedFormats.length; i++ ) 这行,把for循环全部注释掉,再下面加上 <OPTION VALUE="pdf">PDF</OPTION> <OPTION VALUE="xls">E

.Net常用技巧_调用Excel模板格式导出Excel

object missing = System.Reflection.Missing.Value;  //表示缺i省object SaveFileDialog savedialog = new SaveFileDialog(); savedialog.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls"; savedialog.Title = "导成Excel"; if (savedialog.ShowDialog() == Dialog

jsp导出身份证到excel时候格式不对

今天早上客户跟我说excel导出身份证的时候显示有的对有的不对,我一看原来身份证以X结尾的可以,其它都显示不正确.身份正显示如图所示: 在网上搜了一下发现,原来excel看你数字列超过12位就会显示科学计数.而身份证一般都是18位所以显示这样. 解决办法就是在身份证这一列前面加"单引号".形如:'4206211111111111111 具体代码如下:<td style="border: 1px solid #000;">'<s:property v