asp.net mvc 使用NPOI插件导出excel

      /// <summary>
/// 交易账单 导出交易列表
/// </summary>
/// <returns></returns>
public FileResult TranToExcel()
{
Dictionary<string, object> ht = new Dictionary<string, object>();
       //数据源
List<UserTransactionViewModel> list = UserTransactionBLL.GetTransactionListExcelToAdmin(ht,langId);
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
//给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow();
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TransactionNumber.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TransactionTitle.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TransactionUser.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TransactionType.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TransactionState.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.PayType.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TradingCurrency.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.TransactionAmount.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.ExchangeHour.ToLang(langId));
row1.CreateCell().SetCellValue(UserTransactionLangEnum.Remarks.ToLang(langId));
//将数据逐步z写入sheet1各个行
for (int i = ; i < list.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + );
rowtemp.CreateCell().SetCellValue(list[i].Transaction.TranNo);
rowtemp.CreateCell().SetCellValue(list[i].Transaction.Trantitle);
rowtemp.CreateCell().SetCellValue(list[i].UserName);
rowtemp.CreateCell().SetCellValue(Enum.GetName(typeof(TransactionTypeDbEnum), list[i].Transaction.TranTypeId));
rowtemp.CreateCell().SetCellValue(Enum.GetName(typeof(StatusDbEnum), list[i].Transaction.StatusId));
rowtemp.CreateCell().SetCellValue(Enum.GetName(typeof(PayTypeDbEnum), list[i].Transaction.PayTypeId));
rowtemp.CreateCell().SetCellValue(list[i].CurrencyName);
rowtemp.CreateCell().SetCellValue(list[i].Transaction.TranAmount.ToString());
rowtemp.CreateCell().SetCellValue(list[i].Transaction.Addtime.ToString("yyyy-MM-dd HH:mm:ss"));
rowtemp.CreateCell().SetCellValue(list[i].Transaction.Remarks);
}
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(, SeekOrigin.Begin);
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + UserTransactionLangEnum.TransactionHistory.ToLang(langId)+ ".xls";
return File(ms, "application/vnd.ms-excel", fileName); }
上一篇:SAS数据挖掘实战篇【一】


下一篇:Oracle 远程访问配置