net导出excel 并设置格式
2015-09-24 10:00
302 查看
/// <summary> /// 导出Excel /// </summary> /// <param name="dgv">DataGridView对象</param> /// <param name="FileName">文件名, 不带后缀名</param> public void Export(string FileName, List<IncomeStatementOutputDto> list) { Excel.Application excel1 = new Excel.Application(); excel1.DisplayAlerts = false; Excel.Workbook workbook1 = excel1.Workbooks.Add(Type.Missing); excel1.Visible = false; Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook1.Worksheets["sheet1"]; //表头 worksheet1.Cells[1, 1] = "流水号"; //Excel里从第1行,第1列计算 worksheet1.Cells[1, 2] = "日期"; worksheet1.Cells[1, 3] = "对方账户名称"; worksheet1.Cells[1, 4] = "对方银行账户"; worksheet1.Cells[1, 5] = "金额(元)"; worksheet1.Cells[1, 6] = "账户余额(元)"; worksheet1.Cells[1, 7] = "业务说明"; //宽度 ((Range)worksheet1.Cells[1, 1]).EntireColumn.ColumnWidth = 20; ((Range)worksheet1.Cells[1, 2]).EntireColumn.ColumnWidth = 20; ((Range)worksheet1.Cells[1, 3]).EntireColumn.ColumnWidth = 20; ((Range)worksheet1.Cells[1, 4]).EntireColumn.ColumnWidth = 20; ((Range)worksheet1.Cells[1, 5]).EntireColumn.ColumnWidth = 15; ((Range)worksheet1.Cells[1, 6]).EntireColumn.ColumnWidth = 15; ((Range)worksheet1.Cells[1, 7]).EntireColumn.ColumnWidth = 20; //水平位置 ((Range)worksheet1.Columns[1]).HorizontalAlignment = XlHAlign.xlHAlignRight; ((Range)worksheet1.Columns[5]).HorizontalAlignment = XlHAlign.xlHAlignRight; ((Range)worksheet1.Columns[6]).HorizontalAlignment = XlHAlign.xlHAlignRight; ((Range)worksheet1.Rows[1]).HorizontalAlignment = XlHAlign.xlHAlignCenter; ((Range)worksheet1.Rows[1]).Font.Bold = true; for (int i = 0; i < list.Count; i++) { IncomeStatementOutputDto model = list[i]; worksheet1.Cells[i + 2, 1] = "'" + model.SwiftNumber; //Excel里从第1行,第1列计算 worksheet1.Cells[i + 2, 2] = "'" + model.HappenDate; worksheet1.Cells[i + 2, 3] = model.BankName; worksheet1.Cells[i + 2, 4] = "'" + model.EachOtherAccountName; string strMoneyFlg = (_intoStr.Contains(("," + model.OperatingType.ToString() + ",")) ? "+" : "-"); worksheet1.Cells[i + 2, 5] = "'" + strMoneyFlg + model.Money; if (strMoneyFlg == "+") { ((Range)worksheet1.Cells[i + 2, 5]).Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Red); } else { ((Range)worksheet1.Cells[i + 2, 5]).Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Green); } worksheet1.Cells[i + 2, 6] = model.Balance; worksheet1.Cells[i + 2, 7] = model.BusinessDescription; } string fileName = FileName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; string filePath = Server.MapPath("~/" + fileName); workbook1.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); excel1.Workbooks.Close(); excel1.Quit(); int generation = GC.GetGeneration(excel1); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1); excel1 = null; GC.Collect(generation); //打开要下载的文件,并把该文件存放在FileStream中 System.IO.FileStream Reader = System.IO.File.OpenRead(filePath); //文件传送的剩余字节数:初始值为文件的总大小 long Length = Reader.Length; HttpContext.Current.Response.Buffer = false; HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); HttpContext.Current.Response.AddHeader("Content-Length", Length.ToString()); byte[] Buffer = new Byte[10000]; //存放欲发送数据的缓冲区 int ByteToRead; //每次实际读取的字节数 while (Length > 0) { //剩余字节数不为零,继续传送 if (Response.IsClientConnected) { //客户端浏览器还打开着,继续传送 ByteToRead = Reader.Read(Buffer, 0, 10000); //往缓冲区读入数据 HttpContext.Current.Response.OutputStream.Write(Buffer, 0, ByteToRead); //把缓冲区的数据写入客户端浏览器 HttpContext.Current.Response.Flush(); //立即写入客户端 Length -= ByteToRead; //剩余字节数减少 } else { //客户端浏览器已经断开,阻止继续循环 Length = -1; } } //关闭该文件 Reader.Close(); if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath); }
相关文章推荐
- ubuntu让bash文件可以双击运行
- Dave-oracle
- 【Linux】方便的SecureCRT文件上传、下载命令
- Dev - C++简单使用指南
- C++笔记,函数指针
- JVM加载类和weblogic加载类区别
- Linux内核漏洞将影响Haswell架构服务器版本
- JSP中<%@page 四个重要的属性
- jquery操作select(取值,设置选中)
- Java语言中有无穷大
- timer定时器的使用
- dom常用方法(w3c)
- 完全没有基础的安装android studio及导入项目中出现的一些问题
- C#获取文件夹及文件的大小与占用空间的方法
- Floating Action Button(漂浮按钮)
- 2015年秋季个人阅读计划
- 训练深度神经网络的时候需要注意的一些小技巧
- Unable to access the IIS metabase.You do not have sufficient privilege
- Unable to access the IIS metabase.You do not have sufficient privilege
- Unable to access the IIS metabase.You do not have sufficient privilege