zoukankan      html  css  js  c++  java
  • 下载

    public void ExportExcel(DataTable dt)
    {
    if (dt.Rows.Count > 0)
    {
    //创建工作簿
    NPOI.SS.UserModel.IWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
    //创建表
    ISheet sheet = workbook.CreateSheet("vinkong");//表名
    IRow row0 = sheet.CreateRow(0);
    row0.CreateCell(0).SetCellValue("表名");
    row0.CreateCell(1).SetCellValue("字段");
    row0.CreateCell(2).SetCellValue("类型");
    row0.CreateCell(3).SetCellValue("说明");
    for (int r = 0; r < dt.Rows.Count; r++)
    {
    //创建行接受DataTable的行数据
    IRow row = sheet.CreateRow(r + 1);
    row.CreateCell(0).SetCellValue(dt.Rows[r]["TableName"].ToString());
    row.CreateCell(1).SetCellValue(dt.Rows[r]["Field"].ToString());
    row.CreateCell(2).SetCellValue(dt.Rows[r]["Type"].ToString());
    row.CreateCell(3).SetCellValue(dt.Rows[r]["Description"].ToString());
    }
    string filePath = Server.MapPath("~/uploads/" + $"导出数据库表结构_{Guid.NewGuid()}.xls");

    //写文件
    FileStream file = new FileStream(filePath, FileMode.Create);
    workbook.Write(file);
    file.Close();


    //下载
    FileInfo fileInfo = new FileInfo(filePath);
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    Response.AddHeader("Content-Transfer-Encoding", "binary");
    Response.ContentType = "application/octet-stream";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    Response.WriteFile(fileInfo.FullName);
    Response.Flush();
    //删除文件
    System.IO.File.Delete(filePath);

    Response.End();
    }
    else
    {
    Response.Write("<script>alert('导出失败!')</script>");
    }
    }

  • 相关阅读:
    acwing272. 最长公共上升子序列
    哈夫曼编码简单实现
    Linked List Sorting
    jmeter-线程组
    css-书写规范
    mysql-踩坑记录
    vue-npm install
    css-选择器
    js-process对象
    linux-常用命令
  • 原文地址:https://www.cnblogs.com/cxxtreasure/p/13740763.html
Copyright © 2011-2022 走看看