zoukankan      html  css  js  c++  java
  • NPOI 导出Excel

    NPOIExcel npoiexcel = new NPOIExcel();
    string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff");
    npoiexcel.ToExcel(dt, "工作计划", "工作计划", AppDomain.CurrentDomain.BaseDirectory + "Uploads\工作计划" + filename + ".xls");

    System.Web.UI.Page page = new System.Web.UI.Page();

    HttpContext.Current.Response.Clear();

    HttpContext.Current.Response.Buffer = true;

    HttpContext.Current.Response.ContentType = "application/ms-excel";

    HttpContext.Current.Response.ContentEncoding = Encoding.Default;

    HttpContext.Current.Response.Charset = "UTF8";

    // HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());

    //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + page.Server.UrlEncode("../Uploads\报警信息" + filename + ".xls"));
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"");

    HttpContext.Current.Response.WriteFile("../Uploads\工作计划" + filename + ".xls");

    HttpContext.Current.Response.Flush();

    HttpContext.Current.Response.End();

    public class NPOIExcel
    {
    private string _title;
    private string _sheetName;
    private string _filePath;

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

  • 相关阅读:
    AODV点点滴滴
    让控件的DropdownMenu或者PopupMenu弹出来
    如何让CoolBar控件的Bands在同一行上
    怎样在InstallShield的Basic MSI Project中用InstallScript添加路径
    VC调用Delphi制作的动态链接库如何互相传递字符串
    Delphi 中用 GetEnvironmentVariable 获取常用系统变量
    如何在工具栏或者其他的控件上显示其他控件的Hint
    企业信息开发平台(1)序
    对.Net 垃圾回收的C#编程相关方面(Finalize 和Dispose(bool disposing)和 Dispose())的一些理解体会(转)
    android上怎样让一个Service开机自动启动
  • 原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/6005581.html
Copyright © 2011-2022 走看看