• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.NetMvc导出Excel

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//文件流转换

public static Stream DecompressStream(Stream sourceStream)
{
MemoryStream responseStream = new MemoryStream();
using (System.IO.Compression.GZipStream compressedStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Compress, true))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
compressedStream.Write(buffer, 0, buffer.Length);
}
responseStream.Position = 0;
return responseStream;
}


#region 汇款查询导出
public ActionResult DownLoadRemit(string sh, string hy, string fw, string dh, string hybh, string sz, string xjzh, string bh, string start, string end, string way)
{

//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
//设置单元的宽度 
sheet1.SetColumnWidth(0, 20 * 256);
sheet1.SetColumnWidth(1, 20 * 256);
sheet1.SetColumnWidth(2, 20 * 256);
sheet1.SetColumnWidth(3, 20 * 420);
sheet1.SetColumnWidth(4, 20 * 256);
sheet1.SetColumnWidth(5, 20 * 256);
sheet1.SetColumnWidth(6, 20 * 320);
sheet1.SetColumnWidth(7, 20 * 256);
sheet1.SetColumnWidth(8, 20 * 256);
sheet1.SetColumnWidth(9, 20 * 320);
sheet1.SetColumnWidth(10, 20 * 256);
sheet1.SetColumnWidth(11, 20 * 256);
sheet1.SetColumnWidth(12, 20 * 256);
/// 练习合并单元格
sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(4, 4, 4, 4));
//获取list数据
List<RemitBrowseVm> listRainInfo = remit.DCRemit();//Bll建一个方法,查询需要的列名
//给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("编号");
row1.CreateCell(1).SetCellValue("姓名");
row1.CreateCell(2).SetCellValue("汇款类型");
row1.CreateCell(3).SetCellValue("状态");
row1.CreateCell(4).SetCellValue("联系电话");
row1.CreateCell(5).SetCellValue("汇款金额");
row1.CreateCell(6).SetCellValue("付款方式");
row1.CreateCell(7).SetCellValue("确认方式");
row1.CreateCell(8).SetCellValue("付款用途");
row1.CreateCell(9).SetCellValue("登记时间");
row1.CreateCell(10).SetCellValue("审核时间");
row1.CreateCell(11).SetCellValue("审核期数");
row1.CreateCell(12).SetCellValue("财务备注");
//将数据逐步写入sheet1各个行
for (int i = 0; i < listRainInfo.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
rowtemp.CreateCell(0).SetCellValue(listRainInfo[i].hyBh.ToString());
rowtemp.CreateCell(1).SetCellValue(listRainInfo[i].HkName.ToString());
rowtemp.CreateCell(2).SetCellValue(listRainInfo[i].HkYt.ToString());
rowtemp.CreateCell(3).SetCellValue(listRainInfo[i].Zfstate.ToString());
rowtemp.CreateCell(4).SetCellValue(listRainInfo[i].shoujiPhone.ToString());
rowtemp.CreateCell(5).SetCellValue(listRainInfo[i].HkMoney.ToString());
rowtemp.CreateCell(6).SetCellValue(listRainInfo[i].Hkfs.ToString());
rowtemp.CreateCell(7).SetCellValue(listRainInfo[i].QurenFs.ToString());
rowtemp.CreateCell(8).SetCellValue(listRainInfo[i].HkYt.ToString());
rowtemp.CreateCell(9).SetCellValue(listRainInfo[i].Hkdate.ToString());
rowtemp.CreateCell(10).SetCellValue(listRainInfo[i].ZfDate == null ? "" : listRainInfo[i].ZfDate.ToString());
rowtemp.CreateCell(11).SetCellValue(listRainInfo[i].FukuanQs.ToString() == null ? "" : listRainInfo[i].FukuanQs.ToString());
rowtemp.CreateCell(12).SetCellValue(listRainInfo[i].Hkremark == null ? "" : listRainInfo[i].Hkremark.ToString());
}

//创建内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
//调用压缩流方法
Stream NewStream = DecompressStream(ms);
//生成压缩文件,下载
return File(NewStream, "application/zip", "汇款查询" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls.zip");
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net2.0中的MessageBox.Show()方法发布时间:2022-07-10
下一篇:
Asp.netMVC+EF+Spring.Net项目实践(四)发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap