• 下载


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

  • 相关阅读:
    .dll .h .lib等文件的作用与区别
    [转自]语言黑客的福音
    [转载]一个台湾程序员的心历路程
    Servlet学习总结
    会话跟踪(Cookie & Session)
    JSP学习总结
    人往高处走,水往低处流
    GDI 和GDI+ 混合编程
    常用到的知识
    Sqlite3相关
  • 原文地址:https://www.cnblogs.com/cxxtreasure/p/13740763.html
Copyright © 2020-2023  润新知