• ACE JET NPOI


    ACE,JET 使用系统的客户端,必须安装EXCEL,否则,会报错。
    NPOI 客户端不需要安装任何东西。
    对于数据量较大的,NPOI的效率不是ACE,JET可以匹敌的。
    -------------------ACE-----JET----------------------
    虽然现在都普遍使用NPOI进行Excel的import、export,但毕竟当年ACE、JET陪伴我一段时光,今日整理资料偶然发现笔记,就纪念一下

    string fileType = System.IO.Path.GetExtension(txtPath.Text);

            string strOdbcCon = null;          
            if (fileType==".xls") 
            {
    
                strOdbcCon = @"Provider=Microsoft.Jet.OleDb.4.0;Persist Security Info=False;Data Source='" + txtPath.Text + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
            }
            else
            {
                strOdbcCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source='" + txtPath.Text + "';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"; 
            }
             OleDbConnection OleDB = new OleDbConnection(strOdbcCon);
             OleDB.Open();           
            sheetNames = OleDB.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            var names = sheetNames.AsEnumerable().Select(r => r["TABLE_NAME"]).Distinct();
           
            OleDbDataAdapter OleDat = new OleDbDataAdapter(@"select *  from names[0] ", OleDB);
            DataTable dt = new DataTable("table");
            OleDat.Fill(dt);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    /*-------HDR-----HeaderRow
    参数HDR=YES,这代表第一行是标题,不做为数据使用,如果用HDR=NO,则表示第一行不是标题,作为数据使用。系统默认是YES
    参数Excel 8.0 对于EXCEL 97-Excel 2003 版本都用Excel 8.0, 不能访问Excel 2007版本,且不能访问正在打开的Excel文档
    2007以上 都用Extended Properties=Excel 12.0 也可以访问office 97-2003,可以访问正在打开的Excel文件

    IMEX(import Export mode)
    0: is export mode
    1: is import mode
    2: is linked mode (full update capabilities)
    IMEX=0: 汇出模式,这个模式开启的Excel档案只能用来做”写入“用途
    IMEX=1: 汇入模式,这个模式开启的Excel档案只能用来做”读取“用途
    IMEX=2: 连接模式,这个模式开启的Excel档案可以同时支持”读取“与”写入“用途

       /// 
            /// 将excel中的数据导入到DataTable中
            /// 
            /// excel工作薄sheet的名称
            /// excel工作薄sheet第几行开始
            /// 第一行是否是DataTable的列名
            /// 返回的DataTable
            public DataTable ExcelToDataTable(string sheetName, int startrow, bool isFirstRowColumn)
            {
                ISheet sheet = null;
                DataTable data = new DataTable();
                int startRow = startrow;
                int ColumnIndex=0;
                try
                {
                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                        workbook = new XSSFWorkbook(fs);
                    else if (fileName.IndexOf(".xls") > 0) // 2003版本
                        workbook = new HSSFWorkbook(fs);
    
                    if (sheetName != null)
                    {
                        sheet = workbook.GetSheet(sheetName);
                        if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                        {
                            sheet = workbook.GetSheetAt(0);
                        }
                    }
                    else
                    {
                        sheet = workbook.GetSheetAt(startRow);
                    }
                    if (sheet != null)
                    {
                        IRow firstRow = sheet.GetRow(startrow);
                        int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
    
                        if (isFirstRowColumn)
                        {
                            for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                            {
                                ICell cell = firstRow.GetCell(i);
                                if (cell != null)
                                {
                                    string cellValue = cell.StringCellValue;
                                    if (cellValue != null)
                                    {
                                        ColumnIndex=GetColumn(data, cellValue);
                                        if (ColumnIndex > 0)
                                        {
                                            DataColumn column = new DataColumn(cellValue+ColumnIndex);
                                            data.Columns.Add(column);
                                        }
                                        else
                                        {
                                            DataColumn column = new DataColumn(cellValue);
                                            data.Columns.Add(column);
                                        }
                                    }
                                }
                            }
                            startRow = sheet.FirstRowNum + 1;
                        }
                        else
                        {
                            startRow = sheet.FirstRowNum;
                        }
    
                        //最后一列的标号
                        int rowCount = sheet.LastRowNum;
                        for (int i = startRow; i <= rowCount; ++i)
                        {
                            IRow row = sheet.GetRow(i);
                            if (row == null) continue; //没有数据的行默认是null       
    
                            DataRow dataRow = data.NewRow();
                            for (int j = row.FirstCellNum; j < cellCount; ++j)
                            {
                                if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                                    dataRow[j] = row.GetCell(j).ToString();
                            }
                            data.Rows.Add(dataRow);
                        }
                    }
    
                    return data;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    Logger.WriteLog("Code2101" +ex.Message);
                    return null;
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
  • 相关阅读:
    Kafka - 3.x Kafka命令行操作
    无法直接打印变量值,是哪一步出错了吗?
    力扣:110. 平衡二叉树(Python3)
    html如何携带参数自动跳转页面
    外包公司干了2个月,技术倒退两年...
    Android查漏补缺(5)ContentProvider和ContentResolver
    Prometheus监控Linux主机(node-exporter)
    【JavaWeb】-- thymeleaf视图模板技术
    继承-重写
    【21天学习挑战赛—经典算法】LeetCode 26. 删除有序数组中的重复项
  • 原文地址:https://blog.csdn.net/u013400314/article/details/126087073