【发布时间】:2012-06-14 17:12:00
【问题描述】:
我正在尝试使用 C# 读取包含一些合并单元格的 Excel 工作表。我在另一篇文章中读到,从代码的角度来看,合并的单元格仍然存在,但它们只有空值。所以我尝试跳过空值,但我仍然得到不稳定的结果。这是我正在使用的代码。它应该使用电子表格中的值填充 HTML 表:
private void Output_Excel_File()
{
string inputFileLocation = AppDomain.CurrentDomain.BaseDirectory + "/dashboard.xls";
DataSet ds = Get_Spreadsheet_Data(inputFileLocation, "Dashboard Statistics");
if (ds.Tables.Count > 0)
{
foreach (DataTable dt in ds.Tables)
{
int row = 0;
foreach (DataRow dr in dt.Rows)
{
int col = 0;
foreach (DataColumn dc in dt.Columns)
{
string cellValue = dr[dc].ToString();
cellValue = Regex.Replace(cellValue, "\n", "<br /");
if (cellValue == "X" || cellValue == null)
{
col++;
continue;
}
string index = "r" + row.ToString() + "c" + col.ToString();
Literal cellTarget = (Literal)form1.FindControl(index);
cellTarget.Text = cellValue;
col++;
}
row++;
}
}
}
}
特别是我的索引已关闭并且行:
cellTarget.Text = cellValue;
当索引与 HTML 中的索引不匹配时,总是会抛出空引用异常。
我已经用谷歌搜索了又谷歌,但我很困惑。任何建议表示赞赏。
【问题讨论】:
-
Get_Spreadsheet_Data是做什么的?这是图书馆吗?哪一个? -
这段代码是由有经验的人提供给我的。我只是假设它有效。当我调试时,我确实在变量中看到了电子表格中的值,但索引已关闭,我不知道为什么。
标签: c# asp.net html-table import-from-excel