添加引用: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;

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

  1. PowerBuilder中调用NPOI进行Excel导出格式设置示例

    // 功能 :新建excel带边框的单元格,格式为数字并显示为美元货币 // 参数 :ai_row,行号:ai_col,列号 // 返回值 :true/false // 作者 :潮崖之飔 // 日期 ...

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

    问题: 用excel导出数据时,如何设置单元格格式的数字分类为"文本",默认是"常规"? 比如:导出编码0235A089,在Excel查看默认显示的是没有前面的 ...

  3. POI Excel导出样式设置

    HSSFSheet sheet = workbook.createSheet("sheetName");    //创建sheet sheet.setVerticallyCente ...

  4. MyXls导出Excel的各种设置

    MyXls是一个操作Excel的开源类库,支持设置字体.列宽.行高(由BOSSMA实现).合并单元格.边框.背景颜色.数据类型.自动换行.对齐方式等,通过众多项目的使用表现,证明MyXls对于创建简单 ...

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

    1.设置导出格式: webcontent>birt>pages>dialog>ExportReportDialogFragment.jsp页面: 找到for ( int i = ...

  6. POI导入导出Excel(HSSF格式,User Model方式)

    1.POI说明 Apache POI是Apache软件基金会的开源代码库, POI提供对Microsoft Office格式档案读和写的功能. POI支持的格式: HSSF - 提供读写Microso ...

  7. asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)

    //请求一个excel类 Microsoft.Office.Interop.Excel.ApplicationClass excel = null; //创建 Workbook对象 Microsoft ...

  8. 代码中设置excel自定义格式为[红色]的处理方法

    有时候,excel的自定义格式设置时 ,会遇到需要设置为¥#,##0;[红色]¥-#,##0的格式. 其中会带一个颜色标记,但是如果这样的一句代码,放在英文版的Office里面,就失效了,因为英文版应 ...

  9. (转载)DBGridEh导出Excel等格式文件

    DBGridEh导出Excel等格式文件 uses DBGridEhImpExp; {--------------------------------------------------------- ...

随机推荐

  1. 浅析Hadoop文件格式

    Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度为优势.不过,MPP数据库社区也一直批评Hadoop由于文件格式并非为特定目的而建,因此序列化和反序 ...

  2. python分割字符串split,filter函数用法

    现有字符串,需要取出用空格分隔的第一段,操作如下 >>> product_model = ‘WS-C2960G-24TC-L – Fixed Module 0′>>> ...

  3. OD调试篇11

    先看看今天这道菜是用什么语言写的 发现是用VC7.0写的,再随便打开看看 发现未注册以及使用限制,那就用OD载入吧 右击查找所有字符串,找unregistered    找到后双击进入 我们会看见一个 ...

  4. js 网站顶部导航栏

    (function(){ var map = { 'index' : 0, 'gift_code' : 1, 'base_info' : 1, 'band_phone': 1, 'unlink_pho ...

  5. javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")

    原因很简单:因为在js中{}表示一个语句块(代码段),所有加上"()"表示表达式

  6. 数据库update死锁

    比较常见的死锁场景,并发批量update时的一个场景: update cross_marketing set gmtModified = NOW(), pageview = pageview+ #ex ...

  7. POJ 3176 简单DP

    Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16448 Accepted: 10957 Descrip ...

  8. 区间更新 zoj3911

    哎,没什么坑点,一个简单的区间更新题,但是改了好几天没改对,最终还是过了~~发个纪念下 泪奔... #include<cstdio>#include <iostream>#in ...

  9. 我的STL之旅 MyList

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> // ...

  10. maven项目修改java编译版本的方式

    背景 使用 maven 3.x 安装到本地后,创建的项目一般都是基于JDK1.5版本.而目前大多数的项目已经升级到1.6或以上,尤其是Servlet3.0 已经要求Java6或以上版本的环境,往往需要 ...