NPOI读取Excel 数据 转。。。

 
public DataTable am_Decode()
        {
            DataTable table = new DataTable();
            string[] strAscDataList = Directory.GetFileSystemEntries(m_strFilePath);

            foreach (string file in strAscDataList)
            {
                using (FileStream fs = File.OpenRead(file))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(fs);
                    //获取excel的第一个sheet
                    HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);

                    //获取sheet的首行
                    HSSFRow headerRow = (HSSFRow)sheet.GetRow(0);

                    //一行最后一个方格的编号 即总的列数
                    int cellCount = headerRow.LastCellNum;

                    for (int i = headerRow.FirstCellNum; i < cellCount; i++)
                    {
                        DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                        table.Columns.Add(column);
                    }
                    //最后一列的标号  即总的行数
                    int rowCount = sheet.LastRowNum;

                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        HSSFRow row = (HSSFRow)sheet.GetRow(i);
                        DataRow dataRow = table.NewRow();

                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                dataRow[j] = row.GetCell(j).ToString();
                        }

                        table.Rows.Add(dataRow);
                    }
                }
                File.Delete(file);
            }
            return table;
        }

 

posted @ 2014-12-08 15:17  公寓城影子传说  阅读(190)  评论(0编辑  收藏  举报