C#。 xlsx日期单元格由NPOI 2.0导入DataTable

时间:2013-02-23 12:32:47

标签: c# excel apache-poi xlsx npoi

我尝试使用NPOI 2.0库将.xlsx文件转换为DataTable格式。没关系,但我转换为字符串日期单元格有问题。当我尝试使用像row.GetCell(j).ToString()这样的构造时 - 它抛出异常“无法从文本单元格中获取数值”。我试图使用DateCellValue属性,但它也抛出了这个异常。使用其他单元格格式,它的工作正常。 功能,我用它:

private DataTable xlsxToDT(string fileName)
    {
        DataTable table = new DataTable();
        XSSFWorkbook workbook = new XSSFWorkbook(new FileStream(fileName, FileMode.Open, FileAccess.Read));
        ISheet sheet = workbook.GetSheetAt(0);
        IRow headerRow = 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 = (sheet.FirstRowNum); i < sheet.LastRowNum; i++)
        {
            IRow row = sheet.GetRow(i);
            DataRow dataRow = table.NewRow();
            for (int j = row.FirstCellNum; j < cellCount; j++)
            {
                if (row.GetCell(j) != null)
                {
                    //EXCEPTION GENERATING IN THIS CODE
                    dataRow[j] = row.GetCell(j).ToString();
                    ////////////////////////////
                }
            }
            table.Rows.Add(dataRow);
        }
        workbook = null;
        sheet = null;
        return table;
    }

更新: 如果我插入像

这样的代码
row.GetCell(j).SetCellType(CellType.STRING);
问题单元格中的

我的值有“36496.392581018517”。另一个细胞正确转换

4 个答案:

答案 0 :(得分:3)

excel文件的第二列具有日期格式(12/2/1999)。 NPOI在您当前的文化(“ru-RU”)中无法识别此格式。这似乎是NPOI中的一个错误,因为当发生这种情况时,无法从该单元格中读取任何内容。我找到的唯一方法是在读取excel文件之前更改线程的文化(并在之后将其更改):

private DataTable xlsxToDT(string fileName)
{
    var prevCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    try
    {
        // Put your whole method body here.
    }
    finally
    {
        Thread.CurrentThread.CurrentCulture = prevCulture;
    }
}

答案 1 :(得分:1)

ICell cell = row.GetCell(j);
if (cell != null)
{
    switch (cell.CellType)
    {
        case CellType.String:
            dataRow[j] = cell.StringCellValue;
            break;
        case CellType.Numeric:
            dataRow[j] = cell.NumericCellValue;
            break;
        case CellType.Boolean:
            dataRow[j] = cell.BooleanCellValue;
            break;
        default: dataRow[j] = "ERROR";
            break;
    }
}

答案 2 :(得分:0)

我的解决方案是像这样创建函数getValue

FileStream fs = new FileStream(@"\export.xlsx", FileMode.Open);
XSSFWorkbook wb = new XSSFWorkbook(fs);
ISheet sheet = wb.GetSheetAt(0);
// GetValue here
string text = getValueFromCell(sheet.GetRow(row).GetCell(1));

public string getValueFromCell(ICell cell) {
            string output ="";
            if (cell != null)
            {
                switch (cell.CellType)
                {
                    case CellType.String:
                        output = cell.StringCellValue;
                        break;
                    case CellType.Numeric:
                        //dataRow[j] = cell.NumericCellValue;
                        output = cell.NumericCellValue.ToString();
                        break;
                    case CellType.Boolean:
                        output = cell.BooleanCellValue.ToString();
                        //dataRow[j] = cell.BooleanCellValue;
                        break;
                    default:
                        output = "";
                        break;
                }
            }
            return output;
        }

答案 3 :(得分:-1)

这是xls

的正确代码
public DataTable GetDataTableFromExcelFile(string excel_file_Path)
{
    HSSFWorkbook wb;
    NPOI.SS.UserModel.ISheet sh;
    string Sheet_name;

    using (var fs = new FileStream(excel_file_Path, FileMode.Open, FileAccess.Read))
    {
        wb = new HSSFWorkbook(fs);

        Sheet_name = wb.GetSheetAt(0).SheetName;  //get first sheet name
    }
    DataTable DT = new DataTable();
    DT.Rows.Clear();
    DT.Columns.Clear();

    // get sheet
    sh = wb.GetSheet(Sheet_name);

    // add neccessary columns
    if (DT.Columns.Count < sh.GetRow(0).Cells.Count)
    {
        for (int j = 0; j < sh.GetRow(0).Cells.Count; j++)
        {
            DT.Columns.Add(Convert.ToString(sh.GetRow(0).Cells[j]), typeof(string));
        }
    }

    // add row
    DT.Rows.Add();

    int i = 1;
    while (sh.GetRow(i) != null)
    {
        // write row value
        for (int j = 0; j < sh.GetRow(i).Cells.Count; j++)
        {
            var cell = sh.GetRow(i).GetCell(j);

            if (cell != null)
            {
                // TODO: you can add more cell types capatibility, e. g. formula
                switch (cell.CellType)
                {
                    case CellType.Numeric:
                        DT.Rows[i][j] = sh.GetRow(i).GetCell(j).NumericCellValue;
                        //dataGridView1[j, i].Value = sh.GetRow(i).GetCell(j).NumericCellValue;

                        break;
                    case CellType.String:
                        DT.Rows[i-1][j] = sh.GetRow(i).GetCell(j).StringCellValue;

                        break;
                }
            }
        }

        i++;
    }

    return DT;
}