Asp.Net Mvc導出Excel


//文件流轉換

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");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM