C# 导出Excel方法
2014-07-01 22:58
337 查看
private void outPutExcel(System.Data.DataTable dt, System.Data.DataTable dt2) { if (dt == null) return; Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { //clsLog.m_CreateErrorLog("无法创建Excel对象,可能计算机未安装Excel", "", ""); return; } //創建Excel對象 Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); //Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 Microsoft.Office.Interop.Excel.Worksheet worksheet = null; for (int sheetcount = 0; sheetcount < 2; sheetcount++)//循环根据自己需要的sheet的数目这里是两个 { if (worksheet == null) { worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing); } else { worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, worksheet, 1, Type.Missing); } Microsoft.Office.Interop.Excel.Range range = null; if (sheetcount == 0) { long totalCount = dt.Rows.Count; long rowRead = 0; float percent = 0; worksheet.Name = "明細資料一";//第一个sheet在Excel中显示的名称 ////写入标题 for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; range.Interior.ColorIndex = 15;//背景颜色 range.Font.Bold = true;//粗体 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中 //加边框 range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); range.ColumnWidth = 4.63;//设置列宽 range.EntireColumn.AutoFit();//自动调整列宽 //r1.EntireRow.AutoFit();//自动调整行高 } //写入内容 for (int r = 0; r < dt.Rows.Count; r++) { for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i]; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1]; range.Font.Size = 9;//字体大小 //加边框 range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); range.EntireColumn.AutoFit();//自动调整列宽 } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; this.lblCaption.Text = "正在导出数据[" + percent.ToString("0.00") + "%]..."; progressBar1.Value = Convert.ToInt32(percent); System.Windows.Forms.Application.DoEvents(); } range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; if (dt.Columns.Count > 1) { range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; } } else if (sheetcount == 1) { long totalCount = dt2.Rows.Count; long rowRead = 0; float percent = 0; worksheet.Name = "建議解決方案"; ////写入标题 for (int i = 0; i < dt2.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt2.Columns[i].ColumnName; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; range.Interior.ColorIndex = 15;//背景颜色 range.Font.Bold = true;//粗体 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中 //加边框 range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); range.ColumnWidth = 14.63;//设置列宽 range.EntireColumn.AutoFit();//自动调整列宽 //r1.EntireRow.AutoFit();//自动调整行高 } //写入内容 for (int r = 0; r < dt2.Rows.Count; r++) { for (int i = 0; i < dt2.Columns.Count; i++) { worksheet.Cells[r + 2, i + 1] = dt2.Rows[r][i]; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, i + 1]; range.Font.Size = 9;//字体大小 //加边框 range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); range.EntireColumn.AutoFit();//自动调整列宽 } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; //System.Windows.Forms.Application.DoEvents(); this.lblCaption.Text = "正在导出数据[" + percent.ToString("0.00") + "%]..."; progressBar1.Value = Convert.ToInt32(percent); System.Windows.Forms.Application.DoEvents(); } range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; if (dt2.Columns.Count > 1) { range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin; } string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.DateTime.Today.ToString("yyyyMMdd")); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } workbook.SaveAs(path+"aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } } }
相关文章推荐
- c# aspx 导出Excel文件的方法
- C#导出EXCEL没有网格线的解决方法
- C#、javaScript 导出Excel的几种方法!
- C#中导出Excel报表的方法
- C#导出数据到Excel的几种方法
- C#中datatabel导出excel(三种方法)
- C#导出数据到EXCEL方法谈
- C#导出数据到EXCEL方法谈
- C#中导出Excel报表的方法
- [ASP.NET-C#]将DataSet、DataTable中的数据导出为Excel的解决方法
- [置顶]C#中导出Excel报表的方法
- c#导出Excel的方法
- C# 导出数据到Excel后,销毁Excel.exe进程的方法
- C#导出EXCEL没有网格线的解决方法介绍
- C# 导出Excel方法总结
- asp.net中导出到Excel的方法汇总及导出Excel/Csv格式数据最优方案(C#)
- 数据导出到Excel的方法C#
- C# 操作EXCEL 类 可以实现导入导出功能 折腾了好长时间 现在两种方法显示出来