zoukankan      html  css  js  c++  java
  • NPOI根据Excel数据导入导出到Datatable中--帮助类

    public static class ExcelImporter
    {
    /// <summary>
    /// 根据sheet序号获取数据
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="sheetIndex"></param>
    /// <returns></returns>
    public static DataTable ImportByIndex(string fileName, int sheetIndex = 0)
    {
    using (FileStream fs = File.OpenRead(fileName))
    {
    IWorkbook book = null;
    if (fileName.ToLower().EndsWith("xls"))
    book = new HSSFWorkbook(fs);
    if (fileName.ToLower().EndsWith("xlsx"))
    book = new XSSFWorkbook(fs);

    var sheet = book.GetSheetAt(sheetIndex);
    if (sheet == null)
    throw new Exception(@"Can't find sheet at index of {sheetIndex}");

    return sheet.ToTable();
    }
    }

    /// <summary>
    /// 根据sheet名称获取数据
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="sheetName"></param>
    /// <returns></returns>
    public static DataTable ImportBySheetName(string fileName, string sheetName)
    {
    using (FileStream fs = File.OpenRead(fileName))
    {
    IWorkbook book = null;
    if (fileName.ToLower().EndsWith("xls"))
    book = new HSSFWorkbook(fs);
    if (fileName.ToLower().EndsWith("xlsx"))
    book = new XSSFWorkbook(fs);

    var sheet = book.GetSheet(sheetName);
    if (sheet == null)
    throw new Exception(@"Can't find sheet name of {sheetName}");

    return sheet.ToTable();
    }
    }

    /// <summary>
    /// 将sheet转化为DataTable
    /// </summary>
    /// <param name="sheet"></param>
    /// <returns></returns>
    private static DataTable ToTable(this ISheet sheet)
    {
    DataTable dt = new DataTable();
    var firstRow = sheet.GetRow(0);
    if (firstRow != null)
    {
    for (int columnIndex = 0; columnIndex < firstRow.LastCellNum; columnIndex++)
    {
    var cell = firstRow.GetCell(columnIndex);
    dt.Columns.Add(cell.StringCellValue.Trim(), typeof(string));
    }

    for (int rowIndex = 1; rowIndex < sheet.PhysicalNumberOfRows; rowIndex++)
    {
    var row = sheet.GetRow(rowIndex);
    if (row != null)
    {
    DataRow drNew = dt.NewRow();
    for (int columnIndex = 0; columnIndex < firstRow.LastCellNum; columnIndex++)
    {
    drNew[columnIndex] = GetValueType(row.GetCell(columnIndex));
    }
    dt.Rows.Add(drNew);
    }
    }
    }
    return dt;
    }

    /// <summary>
    /// 获取单元格类型
    /// </summary>
    /// <param name="cell"></param>
    /// <returns></returns>
    private static object GetValueType(ICell cell)
    {
    if (cell == null)
    return null;
    switch (cell.CellType)
    {
    case CellType.Blank: //BLANK:
    return null;
    case CellType.Boolean: //BOOLEAN:
    return cell.BooleanCellValue;
    case CellType.Numeric: //NUMERIC:
    short format = cell.CellStyle.DataFormat;
    if (format != 0) { return Convert.ToDateTime(cell.DateCellValue).ToString("yyyy-MM-dd HH:mm:ss"); } else { return cell.NumericCellValue; }
    case CellType.String: //STRING:
    return cell.StringCellValue;
    case CellType.Error: //ERROR:
    return cell.ErrorCellValue;
    case CellType.Formula: //FORMULA:
    default:
    return "=" + cell.CellFormula;
    }
    }
    }

  • 相关阅读:
    A Bug's Life-----poj2492(关系并查集)
    食物链--poj1182(并查集含有关系)
    How Many Answers Are Wrong----hdu3038(并查集)
    Parity game---poj1733
    使用FFmpeg类库实现YUV视频序列编码为视频
    JNA调用DLL函数遇到的几个问题
    从一个MFC工程移植对话框类到另一个MFC工程
    X264 输出的统计值的含义(X264 Stats Output)
    毕业生必知二三事。给即将毕业的师弟师妹看看,很有用的~~~~
    网络视频传输的服务质量(QoS)
  • 原文地址:https://www.cnblogs.com/jobyym/p/NPOI.html
Copyright © 2011-2022 走看看