使用 Gembox.Spreadsheet 提取到数据表时的数据值无效
我正在使用 Gembox 尝试将 Excel 数据导入到应用程序中,但我似乎无法让 ExtractToDataTable
方法正常工作。
异常消息读取
“提取到 DataTable 的 SourceRowIndex:1 和 SourceColumnIndex:4 时数据值无效”
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
string path = openFileDialog1.FileName;
dataGridView1.DataSource = PopulateDataTable(PopulateExcelWorkSheet(path)).DefaultView;
}
public ExcelWorksheet PopulateExcelWorkSheet(string path)
{
path = String.Empty;
// DataTable daTable = new DataTable();
ExcelFile exFile = new ExcelFile();
exFile.LoadXlsx(@"filepath", XlsxOptions.None);
ExcelWorksheet ws = exFile.Worksheets["Sheet1"];
return ws;
}
public DataTable PopulateDataTable(ExcelWorksheet ws)
{
DataTable daTable = CreateDataTable(ws.Rows.Count, ws.Columns.Count);
int rowsInWs = ws.Rows.Count;
int colsInWS = ws.Columns.Count;
try
{
ws.ExtractToDataTable(daTable, rowsInWs, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows[1], ws.Columns[1]);
}
catch {MessageBox.Show("ARGH"); }
string mew = daTable.Rows[0][0].ToString();
int rowCount = daTable.Rows.Count;
int columnCount = daTable.Columns.Count;
string uhm = String.Format("Rows in dataTable : {0} , Columns in dataTable {1} , Rows in WS: {2} , Columns in WS {3}", rowCount.ToString(), columnCount.ToString(), rowsInWs.ToString(), colsInWS.ToString());
MessageBox.Show(uhm);
string mes = ws.Rows[0].Cells[3].Value.ToString();
MessageBox.Show(mes);
return daTable;
}
public DataTable CreateDataTable(int rows, int columns)
{
DataTable skeleton = new DataTable();
for (int i = 0; i <= rows; i++)
{
skeleton.Rows.Add();
}
for (int x = 0; x <= columns; x++)
{
skeleton.Columns.Add();
}
return skeleton;
}
}
I'm using Gembox to try and import Excel data into an app, but I just can't seem to get the ExtractToDataTable
method to work as it should.
The exception message reads
"Invalid Data Value when extracting to DataTable at SourceRowIndex:1 and SourceColumnIndex:4"
Here's my code:
private void button1_Click(object sender, EventArgs e)
{
string path = openFileDialog1.FileName;
dataGridView1.DataSource = PopulateDataTable(PopulateExcelWorkSheet(path)).DefaultView;
}
public ExcelWorksheet PopulateExcelWorkSheet(string path)
{
path = String.Empty;
// DataTable daTable = new DataTable();
ExcelFile exFile = new ExcelFile();
exFile.LoadXlsx(@"filepath", XlsxOptions.None);
ExcelWorksheet ws = exFile.Worksheets["Sheet1"];
return ws;
}
public DataTable PopulateDataTable(ExcelWorksheet ws)
{
DataTable daTable = CreateDataTable(ws.Rows.Count, ws.Columns.Count);
int rowsInWs = ws.Rows.Count;
int colsInWS = ws.Columns.Count;
try
{
ws.ExtractToDataTable(daTable, rowsInWs, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows[1], ws.Columns[1]);
}
catch {MessageBox.Show("ARGH"); }
string mew = daTable.Rows[0][0].ToString();
int rowCount = daTable.Rows.Count;
int columnCount = daTable.Columns.Count;
string uhm = String.Format("Rows in dataTable : {0} , Columns in dataTable {1} , Rows in WS: {2} , Columns in WS {3}", rowCount.ToString(), columnCount.ToString(), rowsInWs.ToString(), colsInWS.ToString());
MessageBox.Show(uhm);
string mes = ws.Rows[0].Cells[3].Value.ToString();
MessageBox.Show(mes);
return daTable;
}
public DataTable CreateDataTable(int rows, int columns)
{
DataTable skeleton = new DataTable();
for (int i = 0; i <= rows; i++)
{
skeleton.Rows.Add();
}
for (int x = 0; x <= columns; x++)
{
skeleton.Columns.Add();
}
return skeleton;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我正在使用的代码,它对我来说工作得很好:
当然,所有内容都作为字符串添加到数据表中,这对于我们的目的来说是完全可以接受的。
Here is the code that I am using that has been working fine for me:
Granted, everything is added as a string to the data table and this is perfectly acceptable for our purposes.
您还可以尝试 DataGridViewConverter,这是一个用于将 DataGridView 导出/导入到GemBox.Spreadsheet 中的 Excel 文件。
您可以在此处找到 DataGridViewConverter 示例。
You can also try DataGridViewConverter, an API for exporting/import DataGridView to Excel file in GemBox.Spreadsheet.
You can find here DataGridViewConverter example.