zoukankan      html  css  js  c++  java
  • NPOIExcel

    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);
            }
        }
  • 相关阅读:
    Linux下几种文件传输命令 sz rz sftp scp
    jqGrid subGrid配置 如何首次加载动态展开所有的子表格
    MySQL使用规范
    Navicat连接MySQL报错2059
    微信小程序
    完美解决 ios10 及以上 Safari 无法禁止缩放的问题
    html5利用getObjectURL获取图片路径上传图片
    Vue的单页应用中如何引用单独的样式文件
    用JS添加和删除class类名
    APP中的 H5和原生页面如何分辨、何时使用
  • 原文地址:https://www.cnblogs.com/sunxuchu/p/5790835.html
Copyright © 2011-2022 走看看