今天来为大家分享的内容是asp.net导出excel,之前做项目的时候遇到了这一点,就在这里拿来写写,分享给需要的人们。
其实,导出excel的方式有很多中,有直接输出html标记的(这种方式实现起来方便,不过导出的数据不太好看),还有直接将数据写入现有excel文件的(这种方式也好实现,不过不能够设置表格的样式(合并表头)),下面我介绍的是使用office程序集导出excel的文件。
ExportExcel
1 / 2 *ASP.NET导出excel详细介绍 3 *需要引入程序集Microsoft.Office.Interop.Excel.dll 4 */ 5 using excel = Microsoft.Office.Interop.Excel; 6 using System.IO; 7 8 namespace demo 9 { 10 public class ExportExcel 11 { 12 public void SaveExcel(string path)//保存路径 13 { 14 if(File.Exists(path)) 15 { 16 File.Delete(path);//如果存在这个文件就删掉 17 } 18 excel.Application excelApp=new excel.Application();//excel应用程序实例 19 excelApp.Application.DisplayAlerts = false;//不弹出警告框 20 excelApp.Application.Workbooks.Add(true);//工作表集合 21 excel.Workbook workbook= excelApp.Workbooks[1];//当前操作的工作表 22 excel.Worksheet worksheet=workbook.ActiveSheet as excel.Worksheet;//当前操作的表格 23 excelApp.Visible = false;//不弹出保存框 24 //设置字体样式 25 excel.Range range = worksheet.get_Range("A1", "E1");//获取设置单元格的范围 26 range.Font.Bold = true; 27 range.Font.Size = 18; 28 range.HorizontalAlignment = excel.XlVAlign.xlVAlignCenter; 29 range.Borders.LineStyle = excel.XlBorderWeight.xlHairline; 30 //合并单元格操作 31 excel.Range range0 = worksheet.get_Range("A1", "B1"); 32 range0.Merge(false);//合并操作 33 worksheet.Cells[0, 1] = "姓名";//设置显示标题 34 excel.Range range0 = worksheet.get_Range("C1", "F1"); 35 range0.Merge(false); 36 worksheet.Cells[0, 2] = "备注"; 37 DataTable dt = GetData();//将从数据库取出来的数据存放到DataTable 38 int row=1;//控制单元格的行数 39 for(int i=0;i<dt.Rows.Count;i++) 40 { 41 worksheet.Cells[row, 1] = dt.Rows[i][0].ToString(); 42 worksheet.Cells[row,2]=dt.Rows[i][1].ToString(); 43 row++; 44 } 45 object missing = System.Reflection.Missing.Value; 46 //文件保存到指定路径 47 workbook.SaveAs(path, missing, missing, missing, missing, missing, excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); 48 workbook.Close(false, path, true); 49 excelApp.Quit(); 50 //释放资源 51 System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); 52 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); 53 System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); 54 worksheet = null; 55 workbook = null; 56 excelApp = null; 57 GC.Collect(); 58 } 59 //将excel下载到本地 60 public void DownloadExcel() 61 { 62 string filepath = Server.MapPath("~/Excel/SaveExcel/person.xls"); 63 string fileName = Path.GetFileName(filepath); 64 FileInfo fileInfo = new FileInfo(filepath); 65 Response.Clear(); 66 Response.ClearContent(); 67 Response.ClearHeaders(); 68 Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 69 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 70 Response.AddHeader("Content-Transfer-Encoding", "binary"); 71 Response.ContentType = "application/octet-stream"; 72 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 73 Response.WriteFile(fileInfo.FullName); 74 Response.Flush(); 75 Response.End(); 76 } 77 } 78 }
看完是否有一定的收获呢?这里还要说明一点,使用的时候别忘了引用Microsoft.Office.Interop.Excel.dll程序集,装了office的朋友就可以直接引用,如果没装的话可以从网上下载一个。今天就写到这里了,希望能给你们提供帮助。谢谢!