【问题标题】:Printing into a label from Excel从 Excel 打印到标签
【发布时间】:2018-02-02 21:08:12
【问题描述】:

我有一个 47 列和大约 100 行的 Excel 表。我有一个带有文本字段、按钮和标签的 Windows 表单应用程序。

这是我想要做的:

当我将一个数字粘贴到文本框中并单击按钮时,它会搜索整个 Excel 工作表并获取该特定字符串在工作表中的行和列(行、列)。这工作正常。我对表演没意见。这就是我真正想做的事情 -

在获得搜索字符串的位置后,我想转到同一行的第 45 列并从该单元格中获取字符串,并将其显示在标签中。

这是我的代码(我所做的一切都没有用!)

string File_name = "C:\\Users\\v-nikken\\Documents\\My Received Files\\Case Wellness.xlsx";
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oWB;
Microsoft.Office.Interop.Excel.Worksheet oSheet;
try
{
    object missing = System.Reflection.Missing.Value;
    oWB = oXL.Workbooks.Open(File_name, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing);
    oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[1];
    Microsoft.Office.Interop.Excel.Range oRng = GetSpecifiedRange(textBox_SRNumber.Text, oSheet);
    if (oRng != null)
    {

        //THIS IS THE LOGIC I HAVE TO TAKE VALUE FROM THE CELL TO THE LABEL
        //AND OBV IT ISN'T WORKING

        int IRPlace = Convert.ToInt32(oRng.Column) + 46; //This is obv wrong

        label_IRMet.Text = Convert.ToString(oSheet.Cells[Convert.ToInt32(oRng.Row), IRPlace]); //This also
        label_scope_sent.Text = IRPlace.ToString();

    }
    else
    {
        MessageBox.Show("Case number not found!", "Please try again");
    }
    oWB.Close(false, missing, missing);

    oSheet = null;
    oWB = null;
    oXL.Quit();
}
catch (Exception ex)
{
}

对格式问题感到抱歉。代码部分由于某种原因无法正常工作。

请帮忙!!

Windows 10 - Excel 2016 - VS 2017 - .net 4.6.1

【问题讨论】:

  • 你试过label_IRMet.Text = Convert.ToString(oSheet.Cells[oRng.Row, IRPlace]);
  • 是的,但它在标签中显示System.__ComObject。有什么猜测吗?你认为代码对吗?

标签: c# .net excel visual-studio windows-applications


【解决方案1】:

Microsoft.Office.Interop.Excel.Range valueForLabel=(Microsoft.Office.Interop.Excel.Range)yourSheet.Cells[oRng.Row,IRPlace]; string labelText=valueForLabel.Value.ToString();

【讨论】:

  • 好的,那我必须做label_IRMet.Text = Convert.ToString(valueForLabel.Text); 并且成功了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多