1. 程式人生 > >文件讀取草稿(excel,csv)

文件讀取草稿(excel,csv)

string guid dwr lda clas isn urn datarow arraylist

using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    public class ExcelHelper
    {

        public static System.Data.DataSet GetTablesFromTxt(string
path, string splitChar, int startLine, string endWith) { int i = 0; System.Collections.ArrayList tablelist = new System.Collections.ArrayList(); System.Data.DataTable table = null; string s = ""; System.Data.DataSet ds = new System.Data.DataSet();
//using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) //{ System.Data.DataTable tmp = null; using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default)) { while (!string.IsNullOrEmpty(s = sr.ReadLine())) {
if (i >= startLine - 1) { string[] list = s.Split(new string[] { splitChar }, StringSplitOptions.None); if (tmp == null) { tmp = new System.Data.DataTable(); //table = new System.Data.DataTable(); foreach (string t in list) { tmp.Columns.Add(new System.Data.DataColumn()); } table = tmp.Clone(); if (!string.IsNullOrEmpty(endWith) && list[0].Contains(endWith)) { break; } var row = table.NewRow(); for (var k = 0; k < list.Length; k++) { row[k] = list[k]; } table.Rows.Add(row); } else { var row = table.NewRow(); for (var k = 0; k < list.Length; k++) { row[k] = list[k]; } table.Rows.Add(row); /* if ((i + 1) % 200000 == 0) { ds.Tables.Add(table); table = new System.Data.DataTable(); table = tmp.Clone(); }*/ } } i++; } if (table.Rows.Count > 0) { ds.Tables.Add(table); } } //} return ds; } /// <summary> /// 讀取指定Excel所有Sheet /// </summary> /// <param name="path">文件路徑</param> /// <returns></returns> public static DataSet ReadDataSet(string path) { DataSet retSet = new DataSet(); using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = WorkbookFactory.Create(stream); var sheetCount = workbook.NumberOfSheets; for (int i = 0; i < sheetCount; i++) { var sheet = workbook.GetSheetAt(i); retSet.Tables.Add(ReadTable(sheet, 0, 0)); } } return retSet; } /// <summary> /// 讀取指定索引Sheet的Excel文件內容,返回DataTable /// </summary> /// <param name="path">excel文件物理路徑</param> /// <param name="sheetIndex">頁簽索引,從0開始</param> /// <param name="titleIndex">表頭索引,從0開始,如果沒有表頭,請填-1,如果表頭在第二行,請填1</param> /// <param name="lastRowDeduction">數據最後一行索引,如果後三行是統計之類的,請填-3</param> /// <returns>返回DataTable,TableName為對應SheetName</returns> public static DataTable ReadTable(string path, int sheetIndex, int titleIndex, int lastRowDeduction) { using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = WorkbookFactory.Create(stream); ISheet sheet = workbook.GetSheetAt(sheetIndex); return ReadTable(sheet, titleIndex, lastRowDeduction); } } private static DataTable ReadTable(ISheet sheet, int titleIndex, int lastRowDeduction) { var retDatTable = new DataTable(); retDatTable.TableName = sheet.SheetName; if (titleIndex < -2) { throw new Exception("無效的表頭索引值!最小值為-1!"); } IRow headerRow = null; var hasHead = true; //無表頭,純數據 if (titleIndex == -1) { headerRow = sheet.GetRow(0);//僅用於取列數用 hasHead = false; } else { headerRow = sheet.GetRow(titleIndex); } if (headerRow == null) { return retDatTable; } int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { //無表頭 if (!hasHead) { retDatTable.Columns.Add("Column" + i); continue; } //處理有表頭的 var cell = headerRow.GetCell(i); var title = string.Empty; if (cell != null) { headerRow.GetCell(i).SetCellType(CellType.String); title = cell.StringCellValue; } else { title = Guid.NewGuid().ToString(); } retDatTable.Columns.Add(title); } //最後一行的標號 即總的行數 int rowCount = sheet.LastRowNum; rowCount += lastRowDeduction; for (int i = (titleIndex + 1 - 1); i <= rowCount; i++) { var row = sheet.GetRow(i); DataRow dataRow = retDatTable.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { var cell = row.GetCell(j); if (cell != null) try { switch (cell.CellType) { case CellType.Numeric: //NPOI中數字和日期都是NUMERIC類型的,這裏對其進行判斷是否是日期類型 if (HSSFDateUtil.IsCellDateFormatted(cell))//日期類型 { dataRow[j] = cell.DateCellValue; } else//其他數字類型 { dataRow[j] = cell.NumericCellValue; } break; case CellType.Formula: IFormulaEvaluator eva = null; var workType = sheet.Workbook.GetType(); if (workType.Name == "XSSFWorkbook") { eva = new XSSFFormulaEvaluator(sheet.Workbook); } else { eva = new HSSFFormulaEvaluator(sheet.Workbook); } dataRow[j] = eva.Evaluate(cell).FormatAsString(); break; case CellType.Blank: dataRow[j] = ""; break; case CellType.Unknown: case CellType.Boolean: case CellType.Error: case CellType.String: dataRow[j] = cell.StringCellValue; ; break; default: break; } } catch { } } retDatTable.Rows.Add(dataRow); } return retDatTable; } } }

文件讀取草稿(excel,csv)