使用 infragistics 检查工作表是否存在
Check if worksheet exist using infragistics
我需要检查 excel 中是否存在工作表,如果不存在,则创建它。我正在使用 infragistics,下面的代码给出了以下错误:
use of unassigned local variable 'workSheet'.
这是代码:
Workbook workbook = new Workbook();
Worksheet workSheet;
foreach (Something result in results)
{
foreach (Something item in result.Something)
{
if (!workbook.Worksheets.Exists(item.GetType().Name))
{
workSheet = workbook.Worksheets.Add(item.GetType().Name);
}
// cell font
IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont();
....
}
}
错误是关于:IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont()
在工作表变量上。
谢谢。
我找到了答案:
Worksheet workSheet = null;
if (!workbook.Worksheets.Exists(item.GetType().Name))
{
workSheet = workbook.Worksheets.Add(item.GetType().Name);
}
else
{
workSheet = workbook.Worksheets[item.GetType().Name];
}
// cell font
IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont();
我必须确保 workSheet 变量具有值(已初始化)。
以下是避免错误的方法。声明时将工作表初始化为 null。在访问工作表之前不要忘记空值检查。
Workbook workbook = new Workbook();
Worksheet workSheet = null;
foreach (Something result in results)
{
foreach (Something item in result.Something)
{
if (!workbook.Worksheets.Exists(item.GetType().Name))
{
workSheet = workbook.Worksheets.Add(item.GetType().Name);
}
if (workSheet != null)
{
// cell font
IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont();
....
}
....
}
}
我需要检查 excel 中是否存在工作表,如果不存在,则创建它。我正在使用 infragistics,下面的代码给出了以下错误:
use of unassigned local variable 'workSheet'.
这是代码:
Workbook workbook = new Workbook();
Worksheet workSheet;
foreach (Something result in results)
{
foreach (Something item in result.Something)
{
if (!workbook.Worksheets.Exists(item.GetType().Name))
{
workSheet = workbook.Worksheets.Add(item.GetType().Name);
}
// cell font
IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont();
....
}
}
错误是关于:IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont()
在工作表变量上。
谢谢。
我找到了答案:
Worksheet workSheet = null;
if (!workbook.Worksheets.Exists(item.GetType().Name))
{
workSheet = workbook.Worksheets.Add(item.GetType().Name);
}
else
{
workSheet = workbook.Worksheets[item.GetType().Name];
}
// cell font
IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont();
我必须确保 workSheet 变量具有值(已初始化)。
以下是避免错误的方法。声明时将工作表初始化为 null。在访问工作表之前不要忘记空值检查。
Workbook workbook = new Workbook();
Worksheet workSheet = null;
foreach (Something result in results)
{
foreach (Something item in result.Something)
{
if (!workbook.Worksheets.Exists(item.GetType().Name))
{
workSheet = workbook.Worksheets.Add(item.GetType().Name);
}
if (workSheet != null)
{
// cell font
IWorkbookFont oFont = workSheet.Workbook.CreateNewWorkbookFont();
....
}
....
}
}