When I working with Excel by C#. If I worked in the same worksheet, it's ok. But when I worked in difference worksheets. It has an error : System.NullReferenceException: 'Object reference not set to an instance of an object.'
Anyone can help me?
Sorry about my English.
Thanks so much ```
private void GetActiveExcelFile()
{
try
{
e = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch (Exception ex)
{
Type type = Type.GetTypeFromProgID("Excel.Application");
e = System.Activator.CreateInstance(type);
};
Microsoft.Office.Interop.Excel.Application oExcel = (Microsoft.Office.Interop.Excel.Application)e;
oWB = oExcel.ActiveWorkbook;
}
bool q1()
{
GetActiveExcelFile();
try
{
Excel.Worksheet esh=new Excel.Worksheet();
esh = oWB.Worksheets["sheet 1"]; //My error here: System.NullReferenceException: Object reference not set to an instance of an object.
if (esh.ListObjects["Table1"].ShowTableStyleRowStripes)
return true;
}
catch { return false }
return false;
}
bool q2()
{
GetActiveExcelFile();
try
{
Excel.Worksheet esh=new Excel.Worksheet();
esh = oWB.Worksheets["sheet 2"];
if (esh.ListObjects["Table2"].ListColumns["Total"].Range.NumberFormat.ToString() == "0.000" ||
esh.Cells.Range["B2:B9"].NumberFormat.ToString() == "0.000" ||
esh.Cells.Range["B1:B9"].NumberFormat.ToString() == "0.000")
return true;
}
catch { return false; }
return false;
}