日韩欧美人妻无码精品白浆,夜夜嗨AV免费入口,国产欧美官网在线看,高校回应聋哑女生因长相完美被质疑

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

[點(diǎn)晴永久免費(fèi)OA]【C#】NPOI導(dǎo)出Excel,包含一個(gè)或者多個(gè)sheet表

admin
2022年11月25日 15:21 本文熱度 2231
文章簡(jiǎn)介:NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒(méi)有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫(xiě)操作。NPOI是一個(gè)開(kāi)源的C#讀寫(xiě)Excel、WORD等微軟OLE2組件文檔的項(xiàng)目。他導(dǎo)出的文件和你電腦右鍵創(chuàng)建的office文件一樣原生,不會(huì)提示格式不符,重要的是不依賴office組件。

1、先在項(xiàng)目中引入NPOI包。

2、建一個(gè)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 填充內(nèi)容
                        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"://浮點(diǎn)型
                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 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved