LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

[点晴永久免费OA]【C#】NPOI导出Excel,包含一个或者多个sheet表

admin
2022年11月25日 15:21 本文热度 1291
文章简介:NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作。NPOI是一个开源的C#读写Excel、WORD等微软OLE2组件文档的项目。他导出的文件和你电脑右键创建的office文件一样原生,不会提示格式不符,重要的是不依赖office组件。

1、先在项目中引入NPOI包。

2、建一个execlHelper.cs类。

using NPOI.XSSF.UserModel;
using System;
using System.Data;
using System.IO;
using System.Web;
using NPOI.SS.UserModel;
namespace C.Customization.Helper
{
    /// <summary>
    /// Class execlHelper.
    /// </summary>
    public class execlHelper
    {
        /// <summary>
        /// Datas the set to excel.
        /// </summary>
        /// <param name="ds">The ds.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static string DataSetToExcel(DataSet ds)
        {
            try
            {
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";
                string urlPath = "Upload/ExcelFiles/";
                string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath);
                Directory.createDirectory(filePath);
                string Path = filePath + fileName;
                FileStream fs = null;
                XSSFWorkbook workbook = new XSSFWorkbook();
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    XSSFSheet sheet = (XSSFSheet)workbook.createSheet(ds.Tables[i].TableName);
                    XSSFCellStyle dateStyle = (XSSFCellStyle)workbook.createCellStyle();
                    XSSFDataFormat format = (XSSFDataFormat)workbook.createDataFormat();
                    dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
                    int rowIndex = 0;
                    #region 新建表,填充表头,填充列头,样式
                    if (rowIndex == 0)
                    {
                        #region 列头及样式
                        XSSFRow headerRow = (XSSFRow)sheet.createRow(0);
                        XSSFCellStyle headStyle = (XSSFCellStyle)workbook.createCellStyle();
                        headStyle.Alignment = HorizontalAlignment.Center;
                        XSSFFont font = (XSSFFont)workbook.createFont();
                        font.FontHeightInPoints = 12;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);
                        //自定义表头
                        for (var j = 0; j < ds.Tables[i].Columns.Count; j++)
                        {
                            sheet.SetColumnWidth(j, 30 * 256);
                            headerRow.createCell(j).SetCellValue(ds.Tables[i].Columns[j].ColumnName);
                            headerRow.GetCell(j).CellStyle = headStyle;
                        }
                        #endregion
                        rowIndex = 1;
                    }
                    #endregion
                    ICellStyle cellstyle = workbook.createCellStyle();
                    cellstyle.VerticalAlignment = VerticalAlignment.Center;
                    cellstyle.Alignment = HorizontalAlignment.Center;
                    foreach (DataRow row in ds.Tables[i].Rows)
                    {
                        XSSFRow dataRow = (XSSFRow)sheet.createRow(rowIndex);
                        #region 填充内容
                        foreach (DataColumn column in ds.Tables[i].Columns)
                        {
                            XSSFCell newCell = (XSSFCell)dataRow.createCell(column.Ordinal);
                            string type = row[column].GetType()?.FullName;
                            newCell.SetCellValue(GetValue(row[column].ToString(), type));
                            newCell.CellStyle = cellstyle;
                        }
                        #endregion
                        rowIndex++;
                    }
                }
                using (fs = File.OpenWrite(Path))
                {
                    workbook.Write(fs);
                    return Path;
                }
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="cellValue">The cell value.</param>
        /// <param name="type">The type.</param>
        /// <returns>System.String.</returns>
        private static string GetValue(string cellValue, string type)
        {
            object value = string.Empty;
            switch (type)
            {
                case "System.String"://字符串类型
                    value = cellValue;
                    break;
                case "System.DateTime"://日期类型
                    System.DateTime dateV;
                    System.DateTime.TryParse(cellValue, out dateV);
                    value = dateV;
                    break;
                case "System.Boolean"://布尔型
                    bool boolV = false;
                    bool.TryParse(cellValue, out boolV);
                    value = boolV;
                    break;
                case "System.Int16"://整型
                case "System.Int32":
                case "System.Int64":
                case "System.Byte":
                    int intV = 0;
                    int.TryParse(cellValue, out intV);
                    value = intV;
                    break;
                case "System.Decimal"://浮点型
                case "System.Double":
                    double doubV = 0;
                    double.TryParse(cellValue, out doubV);
                    value = doubV;
                    break;
                case "System.DBNull"://空值处理
                    value = string.Empty;
                    break;
                default:
                    value = string.Empty;
                    break;
            }
            return value.ToString();
        }
    }
}

该文章在 2022/11/25 15:21:04 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved