C#调用操作Excel的一个类
2004-07-05 13:50
519 查看
CSDN - 文档中心 - .NET | |
标题 | C#调用操作Excel的一个类 cocosoft(原作) |
关键字 | Excel C#调用 |
在这儿本来想写长一点的文章,但因为时间的关系,没有写成。现把自己做的一个小东西,C#调用Excel作报表的源代码放在这儿给大家看看。关于代码的构成,在源代码中已经有完整的代码注释了,这儿就不说什么了。 下面的这个类中,主要完成的功能是从数据库中逐字段读出数据,设置格式后,在Excel中显示出来。这是它运行后的效果图: 在这个类中,有两个参数传进来,一个是它的数据源,另一个是整个报表的标题字符串,具体看代码就应该知道了。 using System; using System.Data; using Excel; namespace LogicLayer { /// <summary> /// OutputExcel 的摘要说明 /// </summary> public class OutputExcel { public OutputExcel(DataView dv,string str) { // // TODO: 在此处添加构造函数逻辑 // Excel.Application excel; int rowIndex=4; int colIndex=1; Excel._Workbook xBk; Excel._Worksheet xSt; excel= new Excel.ApplicationClass();; xBk = excel.Workbooks.Add(true); xSt = (Excel._Worksheet)xBk.ActiveSheet; // //取得标题 // foreach(DataColumn col in dv.Table.Columns) { colIndex++; excel.Cells[4,colIndex] = col.ColumnName; xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐 } // //取得表格中的数据 // foreach(DataRowView row in dv) { rowIndex ++; colIndex = 1; foreach(DataColumn col in dv.Table.Columns) { colIndex ++; if(col.DataType == System.Type.GetType("System.DateTime")) { excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐 } else if(col.DataType == System.Type.GetType("System.String")) { excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString(); xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐 } else { excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString(); } } } // //加载一个合计行 // int rowSum = rowIndex + 1; int colSum = 2; excel.Cells[rowSum,2] = "合计"; xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; // //设置选中的部分的颜色 // xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select(); xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种 // //取得整个报表的标题 // excel.Cells[2,2] = str; // //设置整个报表的标题格式 // xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true; xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22; // //设置报表表格为最适应宽度 // xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select(); xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit(); // //设置整个报表的标题为跨列居中 // xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select(); xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection; // //绘制边框 // xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1; xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗 xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗 xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗 xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;//设置下边线加粗 // //显示效果 // excel.Visible=true; } } } |
相关文章推荐
- 在C#中为DATAGRID控件增加一个删除按钮的一种方法
- AOP C#在行动
- 从C#的编程开始
- 在C#中使用属性控件添加属性窗口
- 在C#中如何将文件放入回收站。
- Visual C# 2005 Express Edition Beta的第一天使用
- C#异步调用
- C#的四个基本技巧
- C#农历控件源码
- 第一章 C#2.0简介
- C#析构器的一个Bug
- C#工具-必备
- C# 2.0 之 partial
- 为什么要用C#
- C# 2.0对现有语法的改进
- C#算法设计与分析-寻找素数
- 将人民币的数字表示转化成大写表示(C#版)
- Software Design Patterns in C#
- The C# Attribute
- c#存取图片