检索Excel工作表单元格值
我正在使用 C# 读取 Excel 工作表特定单元格的值。但根据我的代码,我没有获得任何值。
我获取单元格值的代码是:
foreach (DataRow dr in sourceds.Tables[0].Rows) {
while (clmn < dr.ItemArray.Count()) {
value = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[row,clmn]).Text.ToString();
ws.Cells[row, clmn] = value;
ws.Cells[row, clmn] = value;
clmn++;
}
row++;
}
wb.Save();
这里我正在从其他工作表读取单元格,并希望将该值插入到其他工作表中。
但我没有得到任何“价值”的价值..
I am using c# to read the value of particular cell of excel sheet. But as per my code I am not getting any value..
My code for getting the cell value is:
foreach (DataRow dr in sourceds.Tables[0].Rows) {
while (clmn < dr.ItemArray.Count()) {
value = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[row,clmn]).Text.ToString();
ws.Cells[row, clmn] = value;
ws.Cells[row, clmn] = value;
clmn++;
}
row++;
}
wb.Save();
Here I am reading the cell from other sheet and want to insert that value in other sheet.
but I am not getting any value of "value"..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当内容是文本时,
.Text
参数才会有值。通常您想使用.Value.ToString()
代替。您提到从另一张纸中获取值。你把床单放在哪里?您应该能够执行类似
wb.Sheets[0].Cells[row,clmn].Value.ToString()
的操作(假设您需要第一张工作表中的数据)。查看此代码,您正在逐个单元格地复制项目。使用选择/复制/粘贴功能要容易得多:
The
.Text
parameter will only have a value if the contents are text. Generally you want to use.Value.ToString()
instead.You mentioned getting the value from another sheet. Where do you set the sheet? You should be able to do something like
wb.Sheets[0].Cells[row,clmn].Value.ToString()
(assuming you want data from the first sheet).Looking at this code you are copying items cell by cell. It's much easier to use the Select/Copy/Paste functionality instead:
您可以尝试这个...
您检查该值是否不为空,这样就不会出现异常。 myCel 变量可以是数字(表示列的编号)或字符串(表示列的名称,即“A”)。 myRow 变量是行号。
You can try this...
You check if the value is not null so you don't get an exception. The myCel variable can be either number (indicating the numebr of the column) or string (indicating the name of the column i.e. "A"). myRow variable is the number of the row.
Microsoft.Office.Interop.Excel.dll
。在您的方法中编写以下代码行/或使用此方法:
Microsoft.Office.Interop.Excel.dll
in your application.Write following line of code in your method/or use this method: