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 有一种方法,但它从数据表移动到 excel。excel到数据表是否存在类似简单的东西?

干杯。

4

2 回答 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;
}
于 2013-05-21T10:14:05.703 回答
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/

于 2018-09-04T10:48:49.540 回答