开源的Excel操作项目:

http://www.cnblogs.com/lwme/archive/2011/11/27/2265323.html

添加引用: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.Worksheet[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"];
worksheet1 = (Worksheet)workbook1.Worksheets.Add(Type.Missing, workbook1.Worksheets[1], 1, Type.Missing);
worksheet1.Activate();
worksheet1.Cells[2, 2] = 3455555;
excel1.Visible = true;
excel1.DisplayAlerts = false;//不显示提示框
workbook1.Close(true, "d:\\1.xls", null);
//关闭
worksheet1 = null;
workbook1 = null;
excel1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
excel1 = null;
System.GC.Collect();

关闭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;
worksheet1 = null;
workbook1 = null;
excel1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
excel1 = null;
System.GC.Collect();

重命名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;

C#将DateTable表数据导出到Excel中 
在Visual C#中调用Excel表格,并不像读取Excel表格中的数据那么容易了,因为在Visual C#中调用Excel表格要使用到Excel的COM组件。以VS2005为例,首先添加引用--在COM选项中,添加Microsfot Excel 11.0 Objet Library。
然后在程序中引入命名空间,Using Microsoft.Office.Interop.Excel;
以下面几条语句来介绍一个Excel类中的几个对象。
//实例化一个Excel应用程序对象
Microsoft.Office.Interop.Excel.Application myexcel = new Microsoft.Office.Interop.Excel.Application();
//添加工作表
myexcel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet myworksheet = (Microsoft.Office.Interop.Excel.Worksheet) myexcel.Worksheets["Sheet1"];
//定义一个区域范围
Microsoft.Office.Interop.Excel.Range myrange = myexcel.get_Range(myexcel.Cells[1, 1], myexcel.Cells[3, 3]);
//显示这个excel程序
myexcel.Visible = true ;

但此时的Excel表格是一个空的表格,没有任何内容,下面就来介绍如何往Excel表格中输入数据。
(3).往Excel表格中输入数据:
在命名空间"Excel"中,还定义了一个类"Cell",这个类所代表的就是Excel表格中的一个下单元。通过给差"Cell"赋值,从而实现往Excel表格中输入相应的数据,下列代码功能是打开Excel表格,并且往表格输入一些数据。
myexcel.Caption = "   花名册";
myworksheet .Cells[1, 1] = "  花名册";
myworksheet .Cells[2, 1] = "姓名";
(4). Visual C#调用Excel表格,
了解了上面的这些知识,得到完成上述功能的程序代码就显得比较容易了,函数具体如下:
Excel.ApplicationClass Mylxls = new Excel.ApplicationClass();
Mylxls.Application.Workbooks.Add(true);
myexcel.Caption = "   花名册";
myworksheet .Cells[1, 1] = "  花名册";
myworksheet .Cells[2, 1] = "姓名";
myworksheet .Cells[2, 2] = "性别";
myworksheet .Cells[2, 3] = "出生年月";

//合并单元格(第一行的第一列至第3列)
myworksheet .get_Range(myworksheet .Cells[1, 1], myworksheet .Cells[1, 3]).MergeCells = true;

//逐行写入数据,dt为DataTable对象,从第三行开始写数据。
int i=3;
foreach(DataRow row in dt.Rows)
{
myworksheet .Cells[i, 1] = row["姓名"].ToString();
myworksheet .Cells[i, 2] = row["性别"].ToString();
myworksheet .Cells[i, 3] = row["出生年月"].ToString();
i++;
}

C# excel操作的更多相关文章

  1. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  2. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

  3. C#EXCEL 操作类--C#ExcelHelper操作类

    主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...

  4. Excel 操作类

    转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...

  5. [Excel操作]Microsoft Office Excel 不能访问文件

    最近,客户服务器迁移,因操作系统环境变化而引起的的环境问题一堆,遇到的问题并解决方法在“[Excel]操作”类别会体现. Microsoft Office Excel 不能访问文件“C:\\LMSEx ...

  6. C#常用工具类——Excel操作类

    /// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...

  7. 报表中的Excel操作之Aspose.Cells(Excel模板)

    原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...

  8. C# Excel操作类

    /// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...

  9. Excel操作 Microsoft.Office.Interop.Excel.dll的使用

    ----转载: http://www.cnblogs.com/lanjun/archive/2012/06/17/2552920.html 先说说题外话,前段时间近一个月,我一直在做单据导入功能,其中 ...

随机推荐

  1. div+css的优势在哪?

    1.符合W3C标准.微软等公司都是他的支持者. 2.所搜引擎更加友好. 3.样式调整更加方便. 4.css简洁的代码,减少了带宽. 5.表现和结构分离.在团队开发中更容易分工 并不是取代table,t ...

  2. 区间 (vijos 1439) 题解

    [问题描述] 现给定n个闭区间[ai,bi],1<=i<=n.这些区间的并可以表示为一些不相交的闭区间的并.你的任务就是在这些表示方式中找出包含最少区间的方案.你的输出应该按照区间的升序排 ...

  3. STM32F4_RCC系统时钟配置及描述

    Ⅰ.概述 对于系统时钟应该都知道它的作用,就是驱动整个芯片工作的心脏,如果没有了它,就等于人没有了心跳. 对于使用开发板学习的朋友来说,RCC系统时钟这一块知识估计没怎么去配置过,原因在于开发板提供的 ...

  4. C语言实现冒泡排序法和选择排序法代码参考

    为了易用,我编写排序函数,这和直接在主调函数中用是差不多的. 我认为选择排序法更好理解!请注意 i 和 j ,在写代码时别弄错了,不然很难找到错误! 冒泡排序法 void sort(int * ar, ...

  5. hdu 4631Sad Love Story<计算几何>

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意:依次给你n个点,每次求出当前点中的最近点对,输出所有最近点对的和: 思路:按照x排序,然后用s ...

  6. CDN技术原理

    要了解CDN的实现原理,首先让我们来回顾一下网站传统的访问过程,以便理解其与CDN访问方式之间的差别: 由上图可见,传统的网站访问过程为: 1. 用户在浏览器中输入要访问的域名: 2. 浏览器向域名解 ...

  7. Swift计算文本宽高

    iOS 8 开始可以配合 AutoLayout 自动估算文本的高度,但是当 Cell 比较复杂的时候,还会需要手动去计算.首先声明一个样式 var TextStyle : [String : NSOb ...

  8. shell 与用户交互

    bash shell如何获取命令行参数(添加到命令后的数据).命令行选项(确定命令行为的英文字母).键盘输入数据? 操作命令行参数 1 读取参数 bash shell用位置参数变量(positiona ...

  9. 条款24:若所有的函数参数可能都需要发生类型转换才能使用,请采用non-member函数

    假设有一个有理数类Rational,有一个计算有理数乘法的成员函数operator*,示例如下: #include <iostream> class Rational { public: ...

  10. 34.pad designer警告

    1.Drill hole size is equal or larger than smallest pad size. Pad will be drilled away 原因:钻孔直径太大,直接把p ...