从 Excel 打印到标签中
Printing into a label from Excel
我有一个 excel sheet 有 47 列和大约 100 行。我有一个带有文本字段、按钮和标签的 Windows 表单应用程序。
这是我正在尝试做的事情:
当我将数字粘贴到文本框中并单击按钮时,它会搜索整个 excel sheet 并获取特定字符串在 [=33] 中所在的行和列=](行,列)。这工作正常。我对表演没意见。这是我真正想做的 -
找到搜索到的字符串后,我想转到同一行的第 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
Microsoft.Office.Interop.Excel.Range valueForLabel=(Microsoft.Office.Interop.Excel.Range)yourSheet.Cells[oRng.Row,IRPlace];
string labelText=valueForLabel.Value.ToString();
我有一个 excel sheet 有 47 列和大约 100 行。我有一个带有文本字段、按钮和标签的 Windows 表单应用程序。
这是我正在尝试做的事情:
当我将数字粘贴到文本框中并单击按钮时,它会搜索整个 excel sheet 并获取特定字符串在 [=33] 中所在的行和列=](行,列)。这工作正常。我对表演没意见。这是我真正想做的 -
找到搜索到的字符串后,我想转到同一行的第 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
Microsoft.Office.Interop.Excel.Range valueForLabel=(Microsoft.Office.Interop.Excel.Range)yourSheet.Cells[oRng.Row,IRPlace];
string labelText=valueForLabel.Value.ToString();