- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Gembox 尝试将 Excel 数据导入应用程序,但我似乎无法让 ExtractToDataTable
方法正常工作。
异常信息是这样的
"Invalid Data Value when extracting to DataTable at SourceRowIndex:1 and 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;
}
最佳答案
这是我正在使用的代码,它对我来说运行良好:
private DataTable ReadExcelFile(string flatFilePath, bool firstRowHasHeaders)
{
SpreadsheetInfo.SetLicense("MY KEY");
ExcelFile excelFile = new ExcelFile();
excelFile.LoadXls(flatFilePath);
int unnamed = 0;
int cols;
string[] columns;
int curRow = 0;
int curCol = 0;
DataTable dataTable = new DataTable();
ExcelWorksheet worksheet = excelFile.Worksheets[0];
for (cols = 0; cols < worksheet.Rows[0].AllocatedCells.Count; cols++)
{
if (firstRowHasHeaders)
{
if (worksheet.Rows[0].Cells[cols].Value != null)
dataTable.Columns.Add(worksheet.Rows[0].Cells[cols].Value.ToString());
else
{
dataTable.Columns.Add("Unnamed Column " + (++unnamed));
}
curRow = 1;
}
else
{
dataTable.Columns.Add("Column " + (cols + 1));
}
}
for (; curRow < worksheet.Rows.Count; curRow++)
{
columns = new string[cols];
for (curCol = 0; curCol < cols; curCol++)
{
if (worksheet.Rows[curRow].Cells[curCol].Value == null)
columns[curCol] = "";
else
columns[curCol] = worksheet.Rows[curRow].Cells[curCol].Value.ToString();
}
dataTable.Rows.Add(columns);
}
return dataTable;
}
当然,所有内容都作为字符串添加到数据表中,这对于我们的目的来说是完全可以接受的。
关于c# - 使用 Gembox.Spreadsheet 提取到数据表的无效数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234053/
我正在使用 C# WebApi 2,存储在未安装办公室的 Azure 服务器上。 我需要创建一个 excel 文件,并且知道 GemBox 电子表格不需要在机器上安装 office。 问题是我需要文件
我正在尝试获取电子表格中最后使用的行的索引。我发现在Excel中可以这样做: int lastUsedRow = worksheet.Cells.SpecialCells(Excel.XlCellTy
我正在尝试使用 GemBox 将 excel 文件导入数据表,但我一直收到此错误: 在 SourceRowIndex:1 和 SourceColumnIndex:1 处提取到 DataTable 时,
我第一次使用 GemBox(3.5 版)并且遇到了一个问题。打开生成的 XLSX 文件时,它总是滚动到工作表的底部。我(或者更确切地说,我的客户)希望它从左上角开始。 有没有办法在保存之前以编程方式设
我们如何使用 GemBox.Document 编写或修改 Word 文档中表单控件的现有值? 我找到了 creating 的示例和 reading ,但我就是找不到用于写作的... 我试过用这个: /
我们正在评估 Gembox 作为 Aspose 的替代品。我想我快瞎了,因为我不能做的一件事就是轻松获取 ExcelCell 的地址(例如“B4”)。 例如,当遍历单元格时,我们可能会遇到一个意想不到
我正在使用 Gembox 尝试将 Excel 数据导入应用程序,但我似乎无法让 ExtractToDataTable 方法正常工作。 异常信息是这样的 "Invalid Data Value when
我正在使用一个名为 Gembox 的外部组件来创建 Excel 报告。 将 C# 中的 DateTime 属性导出到 Excel 时,该值在 Excel 中显示为数字(例如“-693593”)。所有其
根据他们的文档:GemBox.Spreadsheet 可以读写公式,但不能计算公式结果。当您在 MS Excel 中打开 XLS 文件时,将自动计算公式结果。 因此,如果我创建一个包含一些注入(inj
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我现在正在使用 GemBox Spreadsheet 来使用 C# 读取 Excel 文件。在 Excel 文件中,有一个单元格包含引用另一个 Excel 文件的日期值。 在 C# 中,起初,我得到的
我的文档有 DOCPROPERTY 字段,我正在尝试更新它们(标题、副标题等)。 Dim title As String = "New Title" Dim subTitle As String =
我有以下问题:当我尝试加载基于 XML 的 Excel 文件 (.xlsx) 时,即使我将 LoadOption 设置为 XlsxDefault 程序也会抛出错误 Reading error: fil
我想使用 C# 加载 .xls 文件(类型:97-2003 电子表格)。我正在使用 Gembox 库。 当我使用下面的命令时,我遇到了“文件包含损坏的数据。”错误。 ExcelFile ef = Ex
我们知道,在 Gembox Spreadsheet 中,您可以为单元格设置边框(使用 C# 语言),例如: worksheet.Cells[row, 2].Style.Borders.SetBorde
我正在使用 .NET CORE,因为我有一台 Mac,我需要使用 GemBox.Spreadsheet,但它仅适用于 .NET Framework。是否可以做一些事情以便将其与 .NET CORE 一
我目前使用 Gembox.Document 来读取 PDF 文档中的内容。我有一个包含所有内容的类库和一个引用它的自托管 .NET Core 3.1 服务。我使用 PDF 数据查询服务,它以内容进行响
我是一名优秀的程序员,十分优秀!