C#读取Excel几种方法的体会

(1) OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不太灵活,不过可以在 DataTable 中对数据进行一些删减修改

这种方式将Excel作为一个数据源,直接用Sql语句获取数据了。所以读取之前要知道此次要读取的Sheet(当然也可以用序号,类似dt.Row[0][0]。这样倒是不需要知道Sheet)

if (fileType == ".xls")
   connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
else
   connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
  
OleDbConnection conn new OleDbConnection(connStr);
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

以上是读取Excel的Sheet名,xls和xlsx的连接字符串也不一样的,可以根据文件的后缀来区别。这里需要注意的一点,Excel里面只有一个Sheet,但通过这种方式读取Sheet可能会大于一个。原因已经有人在别的网站说过了,偷一下懒O(∩_∩)O,下面文段来自【cdwolfling

【在使用过程中发现取出的Sheet和实际excel不一致, 会多出不少。目前总结后有两种情况:

1. 取出的名称中,包括了XL命名管理器中的名称(参见XL2007的公式--命名管理器, 快捷键Crtl+F3);

2. 取出的名称中,包括了FilterDatabase后缀的, 这是XL用来记录Filter范围的, 参见http://www.mrexcel.com/forum/showthread.php?t=27225

对于第一点比较简单, 删除已有命名管理器中的内容即可;第二点处理起来比较麻烦, Filter删除后这些名称依然保留着,简单的做法是新增sheet然后将原sheet Copy进去】

---------------------------------

但实际情况并不能为每个Excel做以上检查,【cdwolfling】也给出了过滤的方案,当时还是有点问题,本来补充了一点。总之先看代码吧

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

{

SheetName = (string)dtSheetName.Rows[i]["TABLE_NAME"];
  
if (SheetName .Contains("$") && !SheetName .Replace("'", "").EndsWith("$"))continue;//过滤无效SheetName完毕....

da.SelectCommand = new OleDbCommand(String.Format(sql_F, tblName), conn);
DataSet dsItem = new DataSet();
da.Fill(dsItem, tblName);

}

因为读取出来无效SheetName一般情况最后一个字符都不会是$。如果SheetName有一些特殊符号,读取出来的SheetName会自动加上单引号,比如在Excel中将SheetName编辑成:MySheet(1),此时读取出来的SheetName就为:'MySheet(1)$',所以判断最后一个字符是不是$之前最好过滤一下单引号。

优点:读取方式简单、读取速度快

缺点:除了读取过程不太灵活之外,这种读取方式还有个弊端就是,当Excel数据量很大时。会非常占用内存,当内存不够时会抛出内存溢出的异常。

不过一般情况下还是非常不错的

 读取Excel完整代码:

/// <summary>
        /// 读取Excel文件到DataSet中
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static DataSet ToDataTable(string filePath)
        {
            string connStr = "";            
            string fileType = System.IO.Path.GetExtension(fileName);
            if (string.IsNullOrEmpty(fileType)) return null;
  
            if (fileType == ".xls")
                connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath+ ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            else
                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath+ ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
            string sql_F = "Select * FROM [{0}]";
  
            OleDbConnection conn = null;
            OleDbDataAdapter da = null;
            DataTable dtSheetName= null;
  
            DataSet ds = new DataSet();
            try
            {
                // 初始化连接,并打开
                conn = new OleDbConnection(connStr);
                conn.Open();
  
                // 获取数据源的表定义元数据                        
                string SheetName = "";
                dtSheetName= conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  
                // 初始化适配器
                da = new OleDbDataAdapter();
                for (int i = 0; i < dtSheetName.Rows.Count; i++)
                {
                    SheetName = (string)dtSheetName.Rows[i]["TABLE_NAME"];
  
                    if (SheetName .Contains("$") && !SheetName .Replace("'", "").EndsWith("$"))
                    {
                        continue;
                    }
  
                    da.SelectCommand = new OleDbCommand(String.Format(sql_F, SheetName ), conn);
                    DataSet dsItem = new DataSet();
                    da.Fill(dsItem, tblName);
  
                    ds.Tables.Add(dsItem.Tables[0].Copy());
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                // 关闭连接
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    da.Dispose();
                    conn.Dispose();
                }
            }
            return ds;
        }

  

(2):Com组件的方式读取Excel

这种方式需要先引用 Microsoft.Office.Interop.Excel 。首选说下这种方式的优缺点

优点:可以非常灵活的读取Excel中的数据

缺点:如果是Web站点部署在IIS上时,还需要服务器机子已安装了Excel,有时候还需要为配置IIS权限。最重要的一点因为是基于单元格方式读取的,所以数据很慢(曾做过试验,直接读取千行、200多列的文件,直接读取耗时15分钟。即使采用多线程分段读取来提高CPU的利用率也需要8分钟。PS:CPU I3)

需要读取大文件的的童鞋们慎重。。。

附上单线程和多线程读取类:

  

(3)NPOI方式读取Excel,NPOI是一组开源的组件,类似Java的 POI。包括:NPOI、NPOI.HPSF、NPOI.HSSF、NPOI.HSSF.UserModel、NPOI.POIFS、NPOI.Util,下载的时候别只下一个噢

 

优点:读取Excel速度较快,读取方式操作灵活性

缺点:只支持03的Excel,xlsx的无法读取。由于这点,使用这种方式的人不多啊,没理由要求客户使用03版Excel吧,再说03版Excel对于行数还有限制,只支持65536行。

(听他们的开发人员说会在2012年底推出新版,支持xlsx的读取。但一直很忙没时间去关注这个事情,有兴趣的同学可以瞧瞧去)

NPOI读取Excel类:

using System;
using System.Data;
using System.IO;
using System.Web;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.POIFS;
using NPOI.Util;
using System.Text;
using System.Configuration;
  
public class NPOIHelper
{
    private static int ExcelMaxRow = Convert.ToInt32(ConfigurationManager.AppSettings["ExcelMaxRow"]);
    /// <summary>
    /// 由DataSet导出Excel
    /// </summary>
    /// <param name="sourceTable">要导出数据的DataTable</param>    
    /// <param name="sheetName">工作表名称</param>
    /// <returns>Excel工作表</returns>    
    private static Stream ExportDataSetToExcel(DataSet sourceDs)
    {
        HSSFWorkbook workbook = new HSSFWorkbook();
        MemoryStream ms = new MemoryStream();
  
        for (int i = 0; i < sourceDs.Tables.Count; i++)
        {
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(sourceDs.Tables[i].TableName);
            HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
            // handling header.            
            foreach (DataColumn column in sourceDs.Tables[i].Columns)
                headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            // handling value.            
            int rowIndex = 1;
            foreach (DataRow row in sourceDs.Tables[i].Rows)
            {
                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
                foreach (DataColumn column in sourceDs.Tables[i].Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                }
                rowIndex++;
            }
        }
        workbook.Write(ms);
        ms.Flush();
        ms.Position = 0;
        workbook = null;
        return ms;
    }
    /// <summary>
    /// 由DataSet导出Excel
    /// </summary>   
    /// <param name="sourceTable">要导出数据的DataTable</param>
    /// <param name="fileName">指定Excel工作表名称</param>
    /// <returns>Excel工作表</returns>    
    public static void ExportDataSetToExcel(DataSet sourceDs, string fileName)
    {
        //检查是否有Table数量超过65325
        for (int t = 0; t < sourceDs.Tables.Count; t++)
        {
            if (sourceDs.Tables[t].Rows.Count > ExcelMaxRow)
            {
                DataSet ds = GetdtGroup(sourceDs.Tables[t].Copy());
                sourceDs.Tables.RemoveAt(t);
                //将得到的ds插入 sourceDs中
                for (int g = 0; g < ds.Tables.Count; g++)
                {
                    DataTable dt = ds.Tables[g].Copy();
                    sourceDs.Tables.Add(dt);
                }
                t--;
            }
        }
  
        MemoryStream ms = ExportDataSetToExcel(sourceDs) as MemoryStream;
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        //HttpContext.Current.Response.End();
        ms.Close();
        ms = null;
    }
    /// <summary>
    /// 由DataTable导出Excel
    /// </summary>
    /// <param name="sourceTable">要导出数据的DataTable</param> 
    /// <returns>Excel工作表</returns>    
    private static Stream ExportDataTableToExcel(DataTable sourceTable)
    {
        HSSFWorkbook workbook = new HSSFWorkbook();
        MemoryStream ms = new MemoryStream();
        HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(sourceTable.TableName);
        HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
        // handling header.      
        foreach (DataColumn column in sourceTable.Columns)
            headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
        // handling value.      
        int rowIndex = 1;
        foreach (DataRow row in sourceTable.Rows)
        {
            HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
            foreach (DataColumn column in sourceTable.Columns)
            {
                dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
            }
            rowIndex++;
        }
        workbook.Write(ms);
        ms.Flush();
        ms.Position = 0;
        sheet = null;
        headerRow = null;
        workbook = null;
        return ms;
    }
    /// <summary>
    /// 由DataTable导出Excel
    /// </summary>
    /// <param name="sourceTable">要导出数据的DataTable</param>
    /// <param name="fileName">指定Excel工作表名称</param>
    /// <returns>Excel工作表</returns>
    public static void ExportDataTableToExcel(DataTable sourceTable, string fileName)
    {
        //如数据超过65325则分成多个Table导出
        if (sourceTable.Rows.Count > ExcelMaxRow)
        {
            DataSet ds = GetdtGroup(sourceTable);
            //导出DataSet
            ExportDataSetToExcel(ds, fileName);
        }
        else
        {
            MemoryStream ms = ExportDataTableToExcel(sourceTable) as MemoryStream;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //HttpContext.Current.Response.End();
            ms.Close();
            ms = null;
        }
    }
  
    /// <summary>
    /// 传入行数超过65325的Table,返回DataSet
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static DataSet GetdtGroup(DataTable dt)
    {
        string tablename = dt.TableName;
  
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);
  
        double n = dt.Rows.Count / Convert.ToDouble(ExcelMaxRow);
  
        //创建表
        for (int i = 1; i < n; i++)
        {
            DataTable dtAdd = dt.Clone();
            dtAdd.TableName = tablename + "_" + i.ToString();
            ds.Tables.Add(dtAdd);
        }
  
        //分解数据
        for (int i = 1; i < ds.Tables.Count; i++)
        {
            //新表行数达到最大 或 基表数量不足
            while (ds.Tables[i].Rows.Count != ExcelMaxRow && ds.Tables[0].Rows.Count != ExcelMaxRow)
            {
                ds.Tables[i].Rows.Add(ds.Tables[0].Rows[ExcelMaxRow].ItemArray);
                ds.Tables[0].Rows.RemoveAt(ExcelMaxRow);
  
            }
        }
  
        return ds;
    }
  
    /// <summary>
    /// 由DataTable导出Excel
    /// </summary>
    /// <param name="sourceTable">要导出数据的DataTable</param>
    /// <param name="fileName">指定Excel工作表名称</param>
    /// <returns>Excel工作表</returns>
    public static void ExportDataTableToExcelModel(DataTable sourceTable, string modelpath, string modelName, string fileName, string sheetName)
    {
        int rowIndex = 2;//从第二行开始,因为前两行是模板里面的内容 
        int colIndex = 0;
        FileStream file = new FileStream(modelpath + modelName + ".xls", FileMode.Open, FileAccess.Read);//读入excel模板
        HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
        HSSFSheet sheet1 = (HSSFSheet)hssfworkbook.GetSheet("Sheet1");
        sheet1.GetRow(0).GetCell(0).SetCellValue("excelTitle");      //设置表头
        foreach (DataRow row in sourceTable.Rows)
        {   //双循环写入sourceTable中的数据
            rowIndex++;
            colIndex = 0;
            HSSFRow xlsrow = (HSSFRow)sheet1.CreateRow(rowIndex);
            foreach (DataColumn col in sourceTable.Columns)
            {
                xlsrow.CreateCell(colIndex).SetCellValue(row[col.ColumnName].ToString());
                colIndex++;
            }
        }
        sheet1.ForceFormulaRecalculation = true;
        FileStream fileS = new FileStream(modelpath + fileName + ".xls", FileMode.Create);//保存
        hssfworkbook.Write(fileS);
        fileS.Close();
        file.Close();
    }
}

  

Config配置了 【ExcelMaxRow】值 65535

 

posted on 2013-10-14 10:13  E8software  阅读(263)  评论(0编辑  收藏  举报

导航