c#Excelpackage将单元格范围读入数据表

问题描述 投票:2回答:2

使用C#我可以设法使用Excel互操作读取单元格范围。但它难以与美元价值和百分比保持一致。所以我正在尝试EEPLus。

FileInfo newFile = new FileInfo(@excelFilePath);
ExcelPackage pck = new ExcelPackage(newFile);
var Summary = workbook1.Worksheets[1];

然后使用以下内容

Convert.ToString(Summary.Cells["I35"].Value);

我得到了不错的价值。

但我想做的是以下内容

 Summary.Cells["E29:P32"].Value

并将其放入数据表。 ImportDataTable有一个方法,但它从datatable移动到excel。对于数据表,excel是否也存在同样简单的东西?

干杯。

c# excel epplus
2个回答
2
投票

没有内置方法,但您可以尝试这样的方法:

var range = summary.Cells["E6:G7"];
DataTable tbl = GetDataTableFromRange(range);

和GetDataTableFromRange方法:

public static DataTable GetDataTableFromRange(ExcelRange range)
{
  DataTable tbl = new DataTable();
  tbl.Columns.Add("Column1");
  tbl.Columns.Add("Column2");
  tbl.Columns.Add("Column3");

  int dataTableColumn = 0;
  DataRow newRow = null;
  int currRow = -1;
  foreach (var item in range)
  {
    if (currRow != item.Start.Row)
    {
      newRow = tbl.NewRow();
      tbl.Rows.Add(newRow);
      dataTableColumn = 0;
      currRow = item.Start.Row;
    }
    newRow[dataTableColumn] = item.Value.ToString();
    dataTableColumn++;
  }
  return tbl;
}

0
投票

你可以这样做:

public bool readXLS(string FilePath)
{
    FileInfo existingFile = new FileInfo(FilePath);
    using (ExcelPackage package = new ExcelPackage(existingFile))
    {
        //get the first worksheet in the workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        int colCount = worksheet.Dimension.End.Column;  //get Column Count
        int rowCount = worksheet.Dimension.End.Row;     //get row count
        string queryString = "INSERT INTO tableName VALUES";        //Here I am using "blind insert". You can specify the column names Blient inset is strongly not recommanded
        string eachVal = "";
        bool status;
        for (int row = 1; row <= rowCount; row++)
        {
            queryString += "(";
            for (int col = 1; col <= colCount; col++)
            {
                eachVal = worksheet.Cells[row, col].Value.ToString().Trim();
                queryString += "'" + eachVal + "',";
            }
            queryString = queryString.Remove(queryString.Length - 1, 1);    //removing last comma (,) from the string
            if (row % 1000 == 0)        //On every 1000 query will execute, as maximum of 1000 will be executed at a time. 
            {
                queryString += ")";
                status = this.runQuery(queryString);    //executing query
                if (status == false)
                    return status;
                queryString = "INSERT INTO tableName VALUES";
            }
            else
            {
                queryString += "),";
            }
        }
        queryString = queryString.Remove(queryString.Length - 1, 1);    //removing last comma (,) from the string
        status = this.runQuery(queryString);    //executing query
        return status;
    }
}

资料来源:http://sforsuresh.in/read-data-excel-sheet-insert-database-table-c/

© www.soinside.com 2019 - 2024. All rights reserved.