博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NPOI读写Excel 或 Microsoft.Office.Interop.Excel 读取excel
阅读量:4212 次
发布时间:2019-05-26

本文共 3885 字,大约阅读时间需要 12 分钟。

相关网址:http://www.cnblogs.com/luxiaoxun/p/3374992.html

方法1:http://www.cnblogs.com/luxiaoxun/p/3374992.html

封装重写; list 导入数据方法:

/// <summary>

        /// 将List数据导入到excel中
        /// </summary>
        /// <param name="data">要导入的数据</param>
        /// <param name="isColumnWritten">DataTable的列名是否要导入</param>
        /// <param name="sheetName">要导入的excel的sheet的名称</param>
        /// <param name="cols">列名集合</param>
        /// <returns>导入数据行数(包含列名那一行)</returns>
        public int ListToExcel<T>(List<T> data, string sheetName, string[] cols, bool isColumnWritten)
        {
            int i = 0;
            int j = 0;
            int count = 0;
            ISheet sheet = null;
            fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                workbook = new XSSFWorkbook();
            else if (fileName.IndexOf(".xls") > 0) // 2003版本
                workbook = new HSSFWorkbook();
            try
            {
                if (workbook != null)
                {
                    sheetName = string.IsNullOrEmpty(sheetName) ? System.IO.Path.GetFileNameWithoutExtension(fileName) : sheetName;
                    sheet = workbook.CreateSheet(sheetName);
                }
                else
                {
                    return -1;
                }
                if (isColumnWritten == true) //写入DataTable的列名
                {
                    IRow row = sheet.CreateRow(0);
                    for (j = 0; j < cols.Length; ++j)
                    {
                        string[] str = cols[j].Split(',');
                        row.CreateCell(j).SetCellValue(str[1]);
                    }
                    count = 1;
                }
                else
                {
                    count = 0;
                }
                Type entityType = data[0].GetType();
                System.Reflection.PropertyInfo[] entityProperties = entityType.GetProperties();
                //将所有entity添加到导出的row中
                foreach (object entity in data)
                {
                    IRow row = sheet.CreateRow(count);
                    //检查所有的的实体都为同一类型
                    if (entity.GetType() != entityType)
                    {
                        throw new Exception("要转换的集合元素类型不一致");
                    }
                    object[] entityValues = new object[entityProperties.Length];
                    for (int m = 0; m < entityProperties.Length; m++)
                    {
                        for (j = 0; j < cols.Length; ++j)
                        {
                            string[] str = cols[j].Split(',');
                            if (entityProperties[m].Name == str[0])
                            {
                                object valuetemp = entityProperties[m].GetValue(entity, null);
                                if (valuetemp != null)
                                {
                                    row.CreateCell(j).SetCellValue(valuetemp.ToString());
                                }
                            }
                        }
                    }
                    ++count;
                }
                workbook.Write(fs); //写入到excel
                Dispose();
                return count;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return -1;
            }
        }

方法2:  Microsoft.Office.Interop.Excel  dll

Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            //Microsoft.Office.Interop.Excel.Workbooks workbooks = ExcelApp.Workbooks;
            //Microsoft.Office.Interop.Excel.Workbook workBook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1];//取得sheet1
            
            //打开一个WorkBook
            Microsoft.Office.Interop.Excel.Workbooks workbooks = ExcelApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workBook = ExcelApp.Workbooks.Open(tf,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //得到WorkSheet对象
            Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets.get_Item(1);
            for (int i = 0; i < workSheet.Rows.Count; i++)
            {
                // Response.Write("row{0}:" + workSheet.Rows[i].ToString());
                for (int j = 0; j < workSheet.Cells.Count; j++)
                {
                    Response.Write("cell[i,j]:" + workSheet.Cells[i, j].ToString());
                }
            }
            return;
            // ExcelApp.Caption ="test";
            //ExcelApp.Workbooks.Open(tf,
            //    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            //    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            //    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            得到WorkSheet对象
            //Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets.get_Item(1);
            //DataTable dt = (DataTable)ExcelApp.Worksheets["Sheet1"];
            //foreach (DataRow item in dt.Rows)
            //{
                
            //}
            ExcelApp.Workbooks.Close();            
             ExcelApp.Quit();
            //ExcelApp.WorkBooks.Open( 'C:\Excel\Demo.xls' ); 
你可能感兴趣的文章
DAG以及任务调度
查看>>
LeetCode——DFS
查看>>
MapReduce Task数目划分
查看>>
ZooKeeper分布式锁
查看>>
3126 Prime Path
查看>>
app自动化测试---ADBInterface驱动安装失败问题:
查看>>
RobotFramework+Eclipse安装步骤
查看>>
测试的分类
查看>>
photoshop cc2019快捷键
查看>>
pycharm2019版本去掉下划线的方法
查看>>
SQL中EXISTS的用法
查看>>
10丨案例:在JMeter中如何设置参数化数据?
查看>>
11丨性能脚本:用案例和图示帮你理解HTTP协议
查看>>
12丨性能场景:做参数化之前,我们需要考虑什么?
查看>>
九度OJ 1091:棋盘游戏 (DP、BFS、DFS、剪枝)
查看>>
九度OJ 1092:Fibonacci (递归)
查看>>
九度OJ 1093:WERTYU (翻译)
查看>>
九度OJ 1094:String Matching(字符串匹配) (计数)
查看>>
九度OJ 1095:2的幂次方 (递归)
查看>>
九度OJ 1471-1480(10/10)
查看>>