NPOI操作Excel

通过NPOI操作excel,包括将dataset中的数据保存到Excel,从Excel中读取数据到dataset中,其中Excel的格式是2003格式,一下是具体代码:

public sealed class Excel
    {
        /// <summary>
        /// 导出数据到Excel
        /// </summary>
        /// <param name="dsExportData"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool ExportDataSetToExcel(DataSet dsExportData, string fileName)
        {
            try
            {
                if (dsExportData == null || dsExportData.Tables.Count == 0) return false;
                HSSFWorkbook hssfworkbook = new HSSFWorkbook();
                foreach (DataTable dt in dsExportData.Tables)
                {
                    ISheet sheet = hssfworkbook.CreateSheet(dt.TableName);
                    IRow rowHeader = sheet.CreateRow(0);
                    //添加列名称
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        rowHeader.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
                        sheet.SetColumnWidth(i, 20*256);
                    }
                    sheet.CreateFreezePane(0, 1, 0, 1);
                    //添加具体每列数据
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        IRow rowData = sheet.CreateRow(i + 1);
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            rowData.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
                        }
                    }
                }
                using (FileStream file = new FileStream(fileName, FileMode.Create))
                {
                    hssfworkbook.Write(file);
                }
                return true;
            }
            catch
            {
                return false;
            }

        }
        /// <summary>
        /// 导入Excel文件到dataset
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static DataSet ImportExcelDateToDataSet(string fileName)
        {
            try
            {
                HSSFWorkbook hssfworkbook = null;
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new HSSFWorkbook(file);
                }
                int sheetCount = hssfworkbook.NumberOfSheets;
                if (sheetCount == 0) return null;
                DataSet dsDataSet = new DataSet();
                for (int i = 0; i < sheetCount; i++)
                {
                    ISheet sheet = hssfworkbook.GetSheetAt(i);
                    //表头
                    var rowHeader = sheet.GetRow(0);
                    if (rowHeader == null || rowHeader.Cells == null || rowHeader.Cells.Count == 0) continue;
                    DataTable dt = new DataTable(sheet.SheetName);
                    for (int j = 0; j < rowHeader.LastCellNum; j++)
                    {
                        dt.Columns.Add(rowHeader.Cells[j].StringCellValue);
                    }
                    var rows = sheet.GetRowEnumerator();
                    rows.MoveNext();
                    while (rows.MoveNext())
                    {
                        IRow row = (HSSFRow)rows.Current;
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < row.LastCellNum; j++)
                        {
                            ICell cell = row.GetCell(j);


                            if (cell == null)
                            {
                                dr[j] = null;
                            }
                            else
                            {
                                dr[j] = cell.ToString();
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                    dsDataSet.Tables.Add(dt);
                }
                return dsDataSet;
            }
            catch
            {
                return null;
            }
        }
    }
Excel操作

 

posted @ 2014-03-17 16:50  一!雨  阅读(245)  评论(0编辑  收藏  举报