MVC小批量数据导出到Excel

下面的lstTitle必须与要导出的数据表字段一一对应

根据个人项目不同,可使用方法,稍微修改就能成为自己的。

这里在后台使用拼接一个table,然后返回到web界面,一个路径的指向就相当于下载保存到本地。(个人理解,见笑/哈哈)

  /// <summary>
        /// 导出excel
        /// 作者:刘建超 2017.10.27
        /// </summary>
        /// <returns></returns>
        public ActionResult LeadOut()
        {
            var query = ExcelList.ExcelList;//这里放入查询得到的List<Model>
            var sbHtml = new StringBuilder();
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            var lstTitle = new List<string> { "年份","员工编号", "中文名"};
            foreach (var columnName in lstTitle)
            {
                sbHtml.AppendFormat(" <td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", columnName);
            }
            sbHtml.Append("</tr>");


            string tdHtml = "<td style='font-size: 12px;height:20px;'>{0}</td>";
            if (query != null && query.Count > 0)
            {
                foreach (var row in query)
                {
                    sbHtml.Append("<tr>");
                    sbHtml.AppendFormat(tdHtml, row.Year);
                    sbHtml.AppendFormat(tdHtml, row.EmployeeNo);
                    sbHtml.AppendFormat(tdHtml, row.Name);
                    sbHtml.Append("</tr>");
                }
                sbHtml.Append("</table>");
            }
            else
            {
                return null;
            }


            string filename = System.IO.Path.Combine(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            string HTMLStr = string.Format("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\"></head><body>{0}</body></html>", sbHtml.ToString());
            byte[] fileContents = Encoding.UTF8.GetBytes(HTMLStr);
            return File(fileContents, "application/ms-excel", filename);
        }




前台页面很简单
<input type="button" value="导出Excel" class="ownDefined" οnclick="LeadOut()" />
 
//导出
    function LeadOut() {
        location.href = "@Url.Action("LeadOut", "CCManagerInfo")";
    }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/// <summary> /// 导出Excel /// </summary> /// <param name="table"></param> /// <returns></returns> public bool ToExcel(DataTable table) { FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); IWorkbook workBook = new HSSFWorkbook(); this._sheetName = this._sheetName.IsEmpty() ? "sheet1" : this._sheetName; ISheet sheet = workBook.CreateSheet(this._sheetName); //处理表格标题 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue(this._title); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1)); row.Height = 500; ICellStyle cellStyle = workBook.CreateCellStyle(); IFont font = workBook.CreateFont(); font.FontName = "微软雅黑"; font.FontHeightInPoints = 17; cellStyle.SetFont(font); cellStyle.VerticalAlignment = VerticalAlignment.Center; cellStyle.Alignment = HorizontalAlignment.Center; row.Cells[0].CellStyle = cellStyle; //处理表格列头 row = sheet.CreateRow(1); for (int i = 0; i < table.Columns.Count; i++) { row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName); row.Height = 350; sheet.AutoSizeColumn(i); } //处理数据内容 for (int i = 0; i < table.Rows.Count; i++) { row = sheet.CreateRow(2 + i); row.Height = 250; for (int j = 0; j < table.Columns.Count; j++) { row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString()); sheet.SetColumnWidth(j, 256 * 15); } } //写入数据流 workBook.Write(fs); fs.Flush(); fs.Close(); return true; } /// <summary> /// 导出Excel /// </summary> /// <param name="table"></param> /// <param name="title"></param> /// <param name="sheetName"></param> /// <returns></returns> public bool ToExcel(DataTable table, string title, string sheetName, string filePath) { this._title = title; this._sheetName = sheetName; this._filePath = filePath; return ToExcel(table); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值