国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

讀寫Excle,不用office環境

2019-11-17 02:04:09
字體:
來源:轉載
供稿:網友

讀寫Excle,不用office環境

1.下載NPOI.dll,并添加引用

2.ExcelHelper幫助類,以下為讀寫的參照方法

        HSSFWorkbook hssfworkbook;          #region  導入Excel 返回Table        public DataTable ImportExcelFile(string filePath)        {            #region//初始化信息            try            {                using (FileStream file = new FileStream(filePath, FileMode.Open, Fileaccess.Read))                {                    hssfworkbook = new HSSFWorkbook(file);                }            }            catch (Exception e)            {                throw e;            }            #endregion            NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();            DataTable dt = new DataTable();            for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)            {                dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());            }            while (rows.MoveNext())            {                HSSFRow row = (HSSFRow)rows.Current;                DataRow dr = dt.NewRow();                for (int i = 0; i < row.LastCellNum; i++)                {                    NPOI.SS.UserModel.ICell cell = row.GetCell(i);                    if (cell == null)                    {                        dr[i] = null;                    }                    else                    {                        dr[i] = cell.ToString();                    }                }                dt.Rows.Add(dr);            }            return dt;        }        #endregion        #region 導出excel        //Datatable導出Excel        public   MemoryStream  GridToExcelByNPOI(DataTable dt)        {            try            {                HSSFWorkbook workbook = new HSSFWorkbook();                ISheet sheet = workbook.CreateSheet("Sheet1");                ICellStyle HeadercellStyle = workbook.CreateCellStyle();                HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;                HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;                HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;                HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;                HeadercellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;                //字體                NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();                headerfont.Boldweight = (short)FontBoldWeight.Bold;                HeadercellStyle.SetFont(headerfont);                //用column name 作為列名                int icolIndex = 0;                IRow headerRow = sheet.CreateRow(0);                foreach (DataColumn item in dt.Columns)                {                    ICell cell = headerRow.CreateCell(icolIndex);                    cell.SetCellValue(item.ColumnName);                    cell.CellStyle = HeadercellStyle;                    icolIndex++;                }                ICellStyle cellStyle = workbook.CreateCellStyle();                //為避免日期格式被Excel自動替換,所以設定 format 為 『@』 表示一率當成text來看                cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;                NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();                cellfont.Boldweight = (short)FontBoldWeight.Normal;                cellStyle.SetFont(cellfont);                //建立內容行                int iRowIndex = 1;                int iCellIndex = 0;                foreach (DataRow Rowitem in dt.Rows)                {                    IRow DataRow = sheet.CreateRow(iRowIndex);                    foreach (DataColumn Colitem in dt.Columns)                    {                        ICell cell = DataRow.CreateCell(iCellIndex);                        cell.SetCellValue(Rowitem[Colitem].ToString());                        cell.CellStyle = cellStyle;                        iCellIndex++;                    }                    iCellIndex = 0;                    iRowIndex++;                }                //自適應列寬度                for (int i = 0; i < icolIndex; i++)                {                    sheet.AutoSizeColumn(i);                }                //寫Excel                MemoryStream  ms = new MemoryStream ();                workbook.Write(ms);                ms.Flush();                ms.Position = 0;                return ms;            }            catch (Exception ex)            {                throw ex;            }        }        #endregion

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建水县| 洛浦县| 明水县| 光泽县| 宣恩县| 大足县| 荥经县| 平果县| 怀集县| 海丰县| 宜君县| 镇赉县| 东丰县| 巴林右旗| 周宁县| 大厂| 武隆县| 柏乡县| 左权县| 靖远县| 平顶山市| 南汇区| 尼玛县| 延庆县| 宜丰县| 雷波县| 乌拉特前旗| 盐山县| 陆丰市| 江川县| 洪湖市| 金昌市| 桐柏县| 盘山县| 桦南县| 同德县| 水富县| 石阡县| 荔波县| 永登县| 东方市|