• 读取Excel文件


    第一种:

    采用OleDB读取EXCEL文件:

      bool hasTitle = false;
                 OpenFileDialog openFile = new OpenFileDialog();
                 openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
                 openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                 openFile.Multiselect = false;
                 openFile.ShowDialog();
                 var filePath = openFile.FileName;
                 string fileType = System.IO.Path.GetExtension(filePath);
       string strCon = string.Format("Provider=Microsoft.ACE.OLEDB.{0}.0;" +
                          "Extended Properties="Excel {1}.0;HDR={2};IMEX=1;";" +
                          "data source={3};",
                          (fileType == ".xls" ? 4 : 12), (fileType == ".xls" ? 8 : 12), (hasTitle ? "Yes" : "NO"), filePath);
                 OleDbConnection myConn = new OleDbConnection(strCon);
                 string strCom = " SELECT * FROM [M_Virgil_Port$]";
                 myConn.Open();
                 OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
                 DataSet myDataSet = new DataSet();
                 myCommand.Fill(myDataSet, "[M_Virgil_Port]");
                 DataTable item = myDataSet.Tables[0];


    第二种:

    NPOI读取EXCEL

     

    1、整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet;行:Row;单元格Cell。

    2、NPOI是POI的C#版本,NPOI的行和列的index都是从0开始

    3、POI读取Excel有两种格式一个是HSSF,另一个是XSSF。

    HSSF和XSSF的区别如下:

    HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format.

    XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.

    即:HSSF适用2007以前的版本,XSSF适用2007版本及其以上的。

       DataTable dt = new DataTable();
                 HSSFWorkbook hssfworkbook = null; //HSSF适用2007以前的版本
                 XSSFWorkbook xssfworkbook = null; //XSSF适用2007版本及其以上的。
                 //string fileExt = Path.GetExtension(strFileName);//获取文件的后缀名
                 using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                 {
                     if (fileType == ".xls")
                         hssfworkbook = new HSSFWorkbook(file);
                     else if (fileType == ".xlsx")
                         xssfworkbook = new XSSFWorkbook(file);//初始化太慢了,不知道这是什么bug
                 }
                 if (hssfworkbook != null)
                 {
                     HSSFSheet sheet = (HSSFSheet)hssfworkbook.GetSheet("Manifest");
                     if (sheet != null)
                     {
                         System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
                         HSSFRow headerRow = (HSSFRow)sheet.GetRow(5);
                         int cellCount = headerRow.LastCellNum;
                         for (int j = 0; j < cellCount; j++)
                         {
                             HSSFCell cell = (HSSFCell)headerRow.GetCell(j);
                             dt.Columns.Add(Convert.ToString(cell));
                         }
                         for (int i = 6; i <= sheet.LastRowNum; i++)
                         {
                             HSSFRow row = (HSSFRow)sheet.GetRow(i);
    
    
                             DataRow dataRow = dt.NewRow();
                             for (int j = row.FirstCellNum; j < cellCount; j++)
                             {
                                 var aa = row.GetCell(j);
                                 if (row.GetCell(j)!=null)
                                     dataRow[j] =Convert.ToString(row.GetCell(j));
                             }
                             dt.Rows.Add(dataRow);
                         }
                     }
                 }
                 else if (xssfworkbook != null)
                 {
                     XSSFSheet xSheet = (XSSFSheet)xssfworkbook.GetSheet("Manifest");
                     if (xSheet != null)
                     {
                         System.Collections.IEnumerator rows = xSheet.GetRowEnumerator();
                         XSSFRow headerRow = (XSSFRow)xSheet.GetRow(0);
                         int cellCount = headerRow.LastCellNum;
                         for (int j = 0; j < cellCount; j++)
                         {
                             XSSFCell cell = (XSSFCell)headerRow.GetCell(j);
                             dt.Columns.Add(cell.ToString());
                         }
                         for (int i = (xSheet.FirstRowNum + 1); i <= xSheet.LastRowNum; i++)
                         {
                            
    
                             XSSFRow row = (XSSFRow)xSheet.GetRow(i);
    
                            
                             DataRow dataRow = dt.NewRow();
                             for (int j = row.FirstCellNum; j < cellCount; j++)
                             {
                                 if (row.GetCell(j) != null)
                                     dataRow[j] = row.GetCell(j).ToString();
                             }
                             dt.Rows.Add(dataRow);
                         }
                     }
                 }
  • 相关阅读:
    随笔一
    UISegmentedControl
    adobe as3 samples
    将flash的文字转换为flash可用的矢量图
    让drawRoundRect抗锯齿的最简单的方法
    AS3和FLEX优化技巧
    Spark project 超级强大的AS3库
    API汇集
    一个as3开发人员的话
    好公司职位要求
  • 原文地址:https://www.cnblogs.com/wlwenjie/p/4530656.html
Copyright © 2020-2023  润新知