检索Excel工作表单元格值

发布于 2024-11-19 09:27:12 字数 471 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惯饮孤独 2024-11-26 09:27:12

仅当内容是文本时,.Text 参数才会有值。通常您想使用 .Value.ToString() 代替。

您提到从另一张纸中获取值。你把床单放在哪里?您应该能够执行类似 wb.Sheets[0].Cells[row,clmn].Value.ToString() 的操作(假设您需要第一张工作表中的数据)。

查看此代码,您正在逐个单元格地复制项目。使用选择/复制/粘贴功能要容易得多:

Range r1 = wb.Sheets[0].Cells["C1", "E5" ]; // define corners of selection square
Range r2 = wb.Sheets[1].Cells["A1"];        // destination
r1.Select();
r1.Copy(r2);

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:

Range r1 = wb.Sheets[0].Cells["C1", "E5" ]; // define corners of selection square
Range r2 = wb.Sheets[1].Cells["A1"];        // destination
r1.Select();
r1.Copy(r2);
不必了 2024-11-26 09:27:12

您可以尝试这个...

MyCellValue = (((Microsoft.Office.Interop.Excel.Range)oWorkSheet.Cells[myRow, myCol]).Value2 != null ? ((Microsoft.Office.Interop.Excel.Range)oWorkSheet.Cells[myRow, myCol]).Value2.ToString().Trim() : "");

您检查该值是否不为空,这样就不会出现异常。 myCel 变量可以是数字(表示列的编号)或字符串(表示列的名称,即“A”)。 myRow 变量是行号。

You can try this...

MyCellValue = (((Microsoft.Office.Interop.Excel.Range)oWorkSheet.Cells[myRow, myCol]).Value2 != null ? ((Microsoft.Office.Interop.Excel.Range)oWorkSheet.Cells[myRow, myCol]).Value2.ToString().Trim() : "");

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.

别忘他 2024-11-26 09:27:12
  1. 在应用程序中添加 Microsoft.Office.Interop.Excel.dll
  2. 在您的方法中编写以下代码行/或使用此方法:

     private void readExcel() 
         {
          对象_行= 1;  
          对象_列= 1;   
          Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();  
          excelApp.Visible = false;  
          excelApp.ScreenUpdating = false;  
          excelApp.DisplayAlerts = false;  
          Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"D:\\Book1.xlsx", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel. XlPlatform.xlWindows, "", true, false, 0, true, false, false);  
          Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
          字符串 currentSheet = "Sheet1";  
          Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);  
          Microsoft.Office.Interop.Excel.Range 范围 = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.UsedRange;  
          字符串 sValue = (range.Cells[_row, _column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();  
          //sValue 有你的值  
         }
    
  1. Add Microsoft.Office.Interop.Excel.dll in your application.
  2. Write following line of code in your method/or use this method:

         private void readExcel() 
         {
          object _row = 1;  
          object _column = 1;   
          Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();  
          excelApp.Visible = false;  
          excelApp.ScreenUpdating = false;  
          excelApp.DisplayAlerts = false;  
          Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"D:\\Book1.xlsx", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);  
          Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
          string currentSheet = "Sheet1";  
          Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);  
          Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.UsedRange;  
          string sValue = (range.Cells[_row, _column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();  
          //sValue has your value  
         }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文