1

我想将数据从我的 excel 表导出到数据表,但不知道如何将数据放入数据表。有人可以帮我吗?

我的代码是:

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(userSelectedFilePath2);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

DataTable excelTb = new DataTable();

for (int i = 0; i <= xlRange.Rows.Count - 1; i++)
{
     for (int j = 1; j <= xlRange.Columns.Count; j++)
     {
          excelTb.Columns.Add(xlRange.Cells[0,j].Value2.ToString());
          foreach (DataRow extb in excelTb.Rows)
          {
              DataRow newDataRow = excelTb.NewRow();
              // Here should be something to put data in DataTable
          }
     }
}
4

4 回答 4

1

试试这个:

  for (int row = 0; row < xlRange.Rows.Count; row++)
    {
        DataRow dataRow = null;
        if (row != 0) dataRow = excelTb.NewRow();

        for (int col = 0; col < xlRange.Columns.Count; col++)
        {
            if (row == 0) //Headers
            {
                excelTb.Columns.Add(xlRange.Cells[row + 1, col + 1].Value2.ToString());
            }
            else //Data rows
            {
                dataRow[col] = xlRange.Cells[row + 1, col + 1].Value2.ToString();

            }
        }
    }
于 2012-11-12T09:46:57.393 回答
0

如果您只需要处理 xlsx 和 xlsm 文件,请使用 epplus,它比互操作快得多。它是一个用于读取/写入 excel 文件的包。此外,它有一个非常好的许可证并且不花钱。

对于您的数据表,您可以沿着这条线走(vb.net codeE)

     Dim tbl As New DataTable
     for cols = 0 to j 'you have to define the columns first
         tbl.Columns.Add()
     next

    for cols = 0 to j
       for rows = 0 to your_rowcount
            Dim row = tbl.NewRow
'Here is the critical part: iterate over the worksheet, and take each value from the sheet. put the value into the datatable. The datatable needs to get a new row for each worksheet.row
    Dim value = yourworksheet.cells(cols+1,rows+1).value
     row(cols)=Value
   next
next
于 2012-11-12T09:45:28.037 回答
0
  private void grabData(string filename)
            {
                    // Clear DataTaable before generating new Table Data/
                    ds.Clear();

                    // Connection String to the previously selected file. 
                    // HDR=Yes advises that spreadsheet has columns. 

                    OleDbConnection con = new OleDbConnection(
                    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename +
                    ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");


                    // The criteria required to Build DataTable. Select specific columns                 //from spreadsheet where teh Site Statuse is LIVE.
                    string strSQL = "SELECT * FROM [YOURTABLE]";

                    // The Command that we will use with our DataAdapter.
                    OleDbCommand cmd = new OleDbCommand(strSQL, con);

                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    da.Fill(ds, "YourTable");


            }
于 2012-11-12T09:51:06.583 回答
0

将工作表中的数据读取到二维对象数组中,如下所示:

//Convert from interop object to native C# object, indexed 1 to length
object[,] data = (object[,])xlWorksheet.UsedRange.Value2;

现在您可以像这样向 DataRow 添加一个单元格:

   for (int row = 1; row < data.GetUpperBound(0); row++)
    {
        DataRow newDataRow = excelTb.NewRow();
        newDataRow["AStringColumn"] = data[row,1].ToString();
        newDataRow["ADoubleColumn"] = Convert.ToDouble(data[row, 2]);
        //repeat for each column OR replace with a column loop and use newDataRow[col] = data[row, col].ToString();

        excelTb.Rows.Add(newDataRow);
    }

请注意,双数组从 1 开始索引,而不是像 C# 中的数组通常从 0 开始索引。

于 2012-11-12T09:55:37.150 回答