c# 向Excel文件写入数据(Workbook 和Worksheet )
2009-12-24 16:29
525 查看
//注意:这个方法在服务器上运行时,服务器必须安装Office //引用 using MyExcel=Microsoft.Office.Interop.Excel; using System.Reflection; public void DbExcel(string filepath,DataTable dt) { //打开excel模板 object missing =Missing.Value; MyExcel.Application excelApp=new Microsoft.Office.Interop.Excel.Application(); excelApp.Application.Workbooks.Open(filepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //填充excel MyExcel._Worksheet sheet = (MyExcel._Worksheet)excelApp.Worksheets[1]; //填充 if (dt.Rows.Count > 0) { int colCount = dt.Columns.Count; object[,] dataArray = new object[dt.Rows.Count+1,colCount]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < colCount; j++) { if (i == 0) { //列名作为第一行 dataArray[i, j] = dt.Columns[j].ColumnName; } dataArray[i + 1, j] = dt.Rows[i][j]; } } MyExcel.Range myRange = sheet.get_Range(sheet.Cells[1,1], sheet.Cells[dt.Rows.Count+1,colCount]); //添加数据 //内容体 myRange.Value2 = dataArray; //设置头样式 //sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, colCount]).Interior.ColorIndex = 7; //设置样式 this.SetWorksheetStyle(sheet, dt.Rows.Count, colCount); } //刷新Pivot table等内容 excelApp.Workbooks[1].RefreshAll(); //保存excel文件 MyExcel.Workbook mybook = excelApp.Workbooks[1]; mybook.Save(); //关闭excel进程 mybook.Close(false, missing, missing); //mybook = null; excelApp.Quit(); //excelApp = null; GC.Collect(); } /// <summary> /// 设置_Worksheet的样式 /// </summary> /// <param name="sheet">Microsoft.Office.Interop.Excel._Worksheet对象</param> /// <param name="rows">总行数</param> /// <param name="columns">总列数</param> public void SetWorksheetStyle(MyExcel._Worksheet sheet,int rows,int columns) { //设置头样式 sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, columns]).Interior.ColorIndex = 7; sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, columns]).Font.Size =17; sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, columns]).Font.Bold=true; //sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, columns]).Font. = 7; //列宽自动 sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, columns]).EntireColumn.AutoFit(); //设置头的高度 MyExcel.Range range=(MyExcel.Range)sheet.Rows[(1).ToString(), System.Type.Missing]; range.RowHeight = 25; sheet.get_Range(sheet.Cells[1, 4], sheet.Cells[rows, 4]).Font.Size = 27; //表框 //sheet.get_Range(sheet.Cells[1, 4], sheet.Cells[rows, 4]).Borders.Weight = 4; //sheet.get_Range(sheet.Cells[1, 4], sheet.Cells[rows, 4]).Borders.ThemeColor = 5; }
相关文章推荐
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#读取EXCEL 文件同时向文件中写入数据和Excel的Range对象
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C# 把DataTable数据写入Excel文件的方法
- C#实现向已存在的Excel文件中写入数据实例(两种方法)
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- ]C#读取Excel数据,文件中某些数据竟然读到DataSet中却为空?
- C# Python 读取txt中的文件,将16进制的数据按照小端方式排列,处理为有符号的数字,并写入text文件;控制台和界面两种方式
- C#读取Excel数据并重新写入Excel
- 将DataGrid数据写入Excel文件
- C#将数据以XML格式写入Excel
- C#读取Excel写入数据库/将 Excel 文件转成 DataTable
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 用C#编程从数据库中读取图片数据导进Excel文件的方法
- c#文件写入与获取post请求数据
- C# 保存Excel数据及文件
- C#把数据写如EXCEL文件,以及如何创建EXCEL文件