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

  • 相关阅读:
    查询是哪个进程占用了特定端口
    (面向c#开发人员) 编写javascript的好习惯一 false 值
    onerror 事件 如何使用 onerror 事件捕获网页中的错误。(chrome、opera、safari 浏览器不支持)
    查找url里面的flag元素判断操作
    jquery 弹出窗
    向上滚动
    kissy helpcenter
    kissyAPI
    IE6 动态创建 iframe 无法显示的 bug 芒果
    图片轮换动画仿GIF
  • 原文地址:https://www.cnblogs.com/cxxtreasure/p/13740763.html
Copyright © 2011-2022 走看看