NPOI与Aspose的导入导出保存

半个月没进博客园了,终于把网站做完了,想想毕业快一年的时间里,都是去学习新的计算,现在也该总结下用到的代码,慢慢整理整理,就先从最简单的导入导出开始吧

一:首先看下国人开发的NPOI的导入导出

项目的Office的操作模块图为:        ,现在贴上导入导出类的代码

(1)导入类

 public class ExcelForImport : IImport

    {

        protected static ExcelForImport _instance;

        private static object lock_instance = new object();

        protected ExcelForImport()

        {

        }

        #region IImport 成员

        public System.Data.DataTable Import(string filepath)

        {

            IWorkbook workBook = this.InitializeWorkbook(filepath);

            IFormulaEvaluator evaluator = new HSSFFormulaEvaluator(workBook);

            ISheet sheet = workBook.GetSheetAt(0);

            IEnumerator rows = sheet.GetEnumerator();

            DataTable dt = new DataTable();

            string[] headerNames = new string[sheet.GetRow(0).PhysicalNumberOfCells];

            for (int j = 0; j < headerNames.Length; j++)

            {

                headerNames[j] = Convert.ToChar(((int)‘A‘) + j % 26).ToString() + ((j / 26) > 0 ? (j / 26).ToString() : string.Empty); // A-Z A1-Z1 An-Zn

            }

            this.AddColumn(dt, headerNames);

            while (rows.MoveNext())

            {

                IRow row = rows.Current as HSSFRow;

                this.AddRow(dt, row, headerNames,evaluator);

            }

            return dt;

        }

        public System.Data.DataTable Import(string filepath, string[] headerNames)

        {

            DataTable dt = new DataTable();

            this.AddColumn(dt, headerNames);

            IWorkbook wb = InitializeWorkbook(filepath);

            IFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);

            ISheet sht = wb.GetSheetAt(0);

            IEnumerator rows = sht.GetRowEnumerator();

            //默认第一行为头部列名

            if (rows.MoveNext())

            {

                while (rows.MoveNext())

                {

                    IRow row = rows.Current as HSSFRow;

                    //M by Duanqh 2012-7-27

                    //if (row == null) continue;

                    this.AddRow(dt, row, headerNames, evaluator);

                }

            }

            return dt;

        }

        #endregion

        //增加Row

        protected void AddRow(DataTable dt, IRow row, string[] headerNames,IFormulaEvaluator evaluator)

        {

            System.Data.DataRow newRow = dt.NewRow();

            for (int i = 0; i < headerNames.Count(); i++)

            {

                newRow[headerNames[i]] = GetHSSFCellValue(evaluator.EvaluateInCell(row.GetCell(i)));

            }

            dt.Rows.Add(newRow);

        }

        //不同数据类型的处理

        protected object GetHSSFCellValue(ICell cell)

        {

            if (cell == null) return string.Empty;

            object rValue = string.Empty;

            switch (cell.CellType)

            {

                case  CellType.NUMERIC:

                    /*

                    if (NPOI.HSSF.UserModel.HSSFDateUtil.IsCellDateFormatted(cell))

                        rValue = cell.DateCellValue.ToString("yyyy-MM-dd HH:mm:ss");

                    else

                        rValue = cell.NumericCellValue.ToString();

                     * */

                    rValue = cell.ToString();

                    break;

                case CellType.STRING:

                    rValue = cell.StringCellValue;

                    break;

                case CellType.BOOLEAN:

                    rValue = cell.BooleanCellValue;

                    break;

                case CellType.FORMULA: //if HSSFFormulaEvaluator.EvaluateInCell(ICell) CellType.FORMULA will never happen

                    rValue = "=" + cell.CellFormula;

                    break;

                case CellType.BLANK:

                default:

                    break;

            }

            return rValue;

        }

        //增加列

        protected void AddColumn(System.Data.DataTable dt, string[] headerNames)

        {

            foreach (string h in headerNames)

            {

                dt.Columns.Add(h);

            }

        }

        //HSSFWorkbook对象

        protected IWorkbook InitializeWorkbook(string path)

        {

            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))

            {

                return new HSSFWorkbook(file);

            }

        }

        //单例

        public static ExcelForImport CreateInstance()

        {

            if (_instance == null)

            {

                lock (lock_instance)

                {

                    if (_instance == null)

                    {

                        _instance = new ExcelForImport();

                    }

                }

            }

            return _instance;

        }

    }

(2)导出类

public class ExcelForExport : IExport

    {

        protected static ExcelForExport _instance;

        private static object lock_instance = new object();

        protected ExcelForExport()

        {

        }

        #region IExport 成员

        public void Export(string filepath, System.Data.DataTable dt)

        {

            if (dt == null)

            {

                throw new ArgumentNullException("The argument of dt is null: ExcelForExport.Export(string filepath, System.Data.DataTable dt)");

            }

            string[] headers = new string[dt.Columns.Count];

            for (int i = 0; i < dt.Columns.Count; i++)

            {

                headers[i] = dt.Columns[i].ColumnName;

            }

            this.Export(filepath, dt, headers);

        }

        public void Export(string filepath, System.Data.DataTable dt, string[] headers)

        {

            IWorkbook newWB = new HSSFWorkbook();

            //ISheet newSht = newWB.CreateSheet("Sheet1");

            this.AddSheet(newWB, dt, headers);

            this.Save(filepath, newWB);

        }

        #endregion

        protected void Save(string filepath, IWorkbook wb)

        {

            using (System.IO.FileStream fs = System.IO.File.Create(filepath))

            {

                wb.Write(fs);

                fs.Close();

            }

        }

        protected void AddSheet(IWorkbook workBook, DataTable dt, string[] headers)

        {

            //头部样式

            ICellStyle hStyle = workBook.CreateCellStyle();

            hStyle.FillForegroundColor = HSSFColor.YELLOW.index;

            hStyle.FillPattern = FillPatternType.BIG_SPOTS;

            hStyle.FillBackgroundColor = HSSFColor.YELLOW.index;

            hStyle.Alignment = HorizontalAlignment.CENTER;

            hStyle.VerticalAlignment = VerticalAlignment.CENTER;

            hStyle.BorderBottom = hStyle.BorderLeft = hStyle.BorderRight = hStyle.BorderTop = BorderStyle.MEDIUM;

            hStyle.BottomBorderColor = hStyle.LeftBorderColor = hStyle.RightBorderColor = hStyle.TopBorderColor = HSSFColor.BLACK.index;

            IFont hFont = workBook.CreateFont();

            hFont.Boldweight = (short)FontBoldWeight.BOLD;

            hStyle.SetFont(hFont);

            int RecordCounts = dt.Rows.Count;

            int PageSize = 65501;

            int TotalPages = (RecordCounts + PageSize - 1) / PageSize;

            for (int i = 1; i <= TotalPages; i++)

            {

                ISheet sheet = workBook.CreateSheet(string.Format("Sheet{0}", i));

                this.AddHeader(sheet, hStyle, headers);

                if (i == TotalPages)

                {

                    this.FillSheet(sheet, dt, PageSize * (i - 1), RecordCounts);

                }

                else

                {

                    this.FillSheet(sheet, dt, PageSize * (i - 1), PageSize * i);

                }

            }

        }

        protected void FillSheet(ISheet sheet, DataTable dt, int rowIndexStart, int rowIndexEnd)

        {

            for (int i = rowIndexStart; i < rowIndexEnd; i++)

            {

                object[] columns = dt.Rows[i].ItemArray;

                this.AddRow(sheet, columns);

            }

            #region

            /*

             * 自动调整列的宽度 只支持数字和英文 不支持中文

             * 中文解决方案 遍历获取列中最大宽度

             * http://blog.csdn.net/jerry_cool/article/details/7000085

             * */

            #endregion

            for (int i = 0; i < dt.Columns.Count; i++)

            {

                sheet.AutoSizeColumn(i);

            }

        }

        protected void AddRow(ISheet sht, object[] columns)

        {

            this.AddRow(sht, columns, sht.LastRowNum + 1);

        }

        protected void AddRow(ISheet sht, object[] columns, int rowindex)

        {

            IRow hRow = sht.CreateRow(rowindex);

            hRow.Height = 100 * 4;

            int cellIndex = 0;

            foreach (object c in columns)

            {

                ICell hCell = hRow.CreateCell(cellIndex++);

                hCell.SetCellValue(c.ToString());

            }

        }

        protected void AddHeader(ISheet sht, ICellStyle hStyle, string[] headers)

        {

            this.AddHeader(sht, hStyle, headers, 0);

        }

        //增加文件头,包含样式

        protected void AddHeader(ISheet sht, ICellStyle hStyle, string[] headers, int rowindex)

        {

            IRow hRow = sht.CreateRow(rowindex);

            hRow.Height = 200 * 3;

            int cellIndex = 0;

            foreach (string h in headers)

            {

                ICell hCell = hRow.CreateCell(cellIndex++);

                hCell.CellStyle = hStyle;

                hCell.SetCellValue(h);

            }

        }

        //单例

        public static ExcelForExport CreateInstance()

        {

            if (_instance == null)

            {

                lock (lock_instance)

                {

                    if (_instance == null)

                    {

                        _instance = new ExcelForExport();

                    }

                }

            }

            return _instance;

        }

    }

(3)上面的导出都是以DataTable的形式导出,写一个转换方法,后面aspose插件的导入导出也就列举DataTable类型的,需要什么类型的都可以自己转换

public static DataTable CopyToDataTable<T>(this IEnumerable<T> list)

        {

            var pList = new List<PropertyInfo>();

            var type = typeof(T);

            var dt = new DataTable();

            Array.ForEach(type.GetProperties(), p =>

            {

                pList.Add(p);

                dt.Columns.Add(p.Name);

            });

            foreach (var item in list)

            {

                DataRow row = dt.NewRow();

                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));

                dt.Rows.Add(row);

            }

            return dt;

        }

二:Aspose插件的导入导出

导入:

public static DataTable Import(String strFileName) 

        { 

            Workbook book = new Workbook(); 

           book.Open(strFileName); 

            Worksheet sheet = book.Worksheets[0]; 

            Cells cells = sheet.Cells; 

           return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + cells.MaxDataColumn + 1, true); 

       }

导出:

private static void Export<T>(IEnumerable<T> data, HttpResponse response) 

       { 

          Workbook workbook = new Workbook(); 

           Worksheet sheet = (Worksheet)workbook.Worksheets[0];                         

            PropertyInfo[] ps = typeof(T).GetProperties(); 

            var colIndex = "A"; 

           foreach (var p in ps) 

           { 

                  sheet.Cells[colIndex + 1].PutValue(p.Name); 

                  int i = 2; 

                  foreach (var d in data) 

                  { 

                      sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null)); 

                      i++; 

                  } 

                  colIndex = ((char)(colIndex[0] + 1)).ToString(); 

           } 

           response.Clear(); 

           response.Buffer = true; 

           response.Charset = "utf-8"; 

           response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls"); 

           response.ContentEncoding = System.Text.Encoding.UTF8; 

           response.ContentType = "application/ms-excel"; 

           response.BinaryWrite(workbook.SaveToStream().ToArray()); 

           response.End(); 

       }

三:项目中报表的生成,主要是存储过程的问题,其他就是调用上面的2个控件给单元格填值的操作,而且我基本都是用的书签去插值,没什么可以借鉴的,下一篇我将总结下,怎么学习写存储过程,怎么对存储过程的空间与效率取舍的认识

四:快一年了,太多对新东西的渴望,换了家公司,也进入了正轨,也需要总结下一年下来学习到的一切,该巩固下了

时间: 2024-11-08 15:00:09

NPOI与Aspose的导入导出保存的相关文章

[Cyan之旅]使用NPOI实现Excel的导入导出,踩坑若干.

Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作. 来自:

【NPOI】.NET EXCEL导入导出开发包

1.导出 //工作簿HSSFWorkbook HSSFWorkbook hssfworkbook = new HSSFWorkbook(); //ISheet页 ISheet sheet1 = hssfworkbook.CreateSheet("员工资料"); //创建行 IRow rowHeader = sheet1.CreateRow(0); //设置第一行中的每一个单元格 rowHeader.CreateCell(0, CellType.STRING).SetCellValue(

Aspose.Cells导入导出

插件:Aspose.Cells 没有安装office插件也能使用: 导出:不能使用ajax异步· /// <summary> /// 导出试题 /// </summary> /// <param name="userId">用户Id</param> /// <param name="courseId">课程Id</param> /// <returns></returns>

NPOI实现Excel导入导出

NPOI实现Excel的导入导出,踩坑若干. Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对

Aspose.Cells.dll引用导入导出Excel

Aspose.Cells 导入导出EXCEL 文章出处:http://hi.baidu.com/leilongbing/item/c11467e1819e5417595dd8c1 修改样式       Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells;//单元格 //样式2 Aspose.Cells.Style st

使用NPOI导入导出标准Excel

尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 直接导出html,修改后缀名为.xls,这个方法有点像骗人的把戏,而且不能再导入 使用Jet OLEDB引擎来进行导入导出,完全使用sql语句来进行操作,缺点能控制的东西非常有限,比如格式就难以控制 使用Office COM组件进行导入导出,对环境依赖性太强(如"检索 COM 类工厂-"错

NPOI导入导出数据

//导入数据 protected void btnImport_Click(object sender, EventArgs e) { //判断fileImport控件中是否有需要上传的文件 if (this.fileImport.HasFile) { //判断文件类型是否符合要求   if (Path.GetExtension(this.fileImport.FileName) != ".xls") { MessageTips("上传的文件类型不符合要求", Me

使用NPOI库导入导出EXCEL

一.EXCEL  导入(Excel 导入导出实际项目中会被封装成**Helper 本示例只对简单功能做演示) NPOI 包引用 视图view @{ ViewBag.Title = "NPOIExcel"; } <h2>NPOIExcel</h2> <form action="@Url.Action("NPOIInport", "Home")" method="post" enc

aspose.cells excel表格导入导出

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; using System.IO; using Aspose.Cells; using System.Data; using System.ComponentModel; using System.Configuration; namespace src.Common { publ