OpenXML如何检查工作表中的行是否为空 [英] OpenXML how to check if a row is empty in a sheet
本文介绍了OpenXML如何检查工作表中的行是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查电子表格中标题行旁边的行是否为空.如果为空,我要删除工作表.到目前为止,我只能让它查看单元格A2并检查其是否为空,但是有没有办法检查整行?
I want to check if a row beside the header row is empty in a spreadsheet. If it's empty, I want to delete the sheet. So far I can only get it to look at cell A2 and check if it empty, but is there a way to check the entire row?
谢谢
private void CheckForEmptyRow(WorkbookPart wbp, string sheetId)
{
Sheet sheet = wbp.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Id == sheetId);
WorksheetPart wsp = (WorksheetPart).(wbp.GetPartById(sheetId));
Cell cell = wsp.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "A2").FirstOrDefault();
If (cell == null)
{
sheet.Remove();
wbp.DeletePart(wsp);
}
}
推荐答案
您可以像这样循环遍历每一行并检查该行是否为空:
You can loop through each row and check if the row is null like this:
private void CheckForEmptyRow(WorkbookPart wbp, string sheetId)
{
Sheet sheet = wbp.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Id == sheetId);
WorksheetPart wsp = (WorksheetPart).(wbp.GetPartById(sheetId));
IEnumerable<SheetData> sheetData = wsp.Worksheet.Elements<SheetData>();
bool isRowEmpty = false;
foreach (SheetData SD in sheetData)
{
IEnumerable<Row> row = SD.Elements<Row>(); // Get the row IEnumerator
if (row == null)
{
isRowEmpty = true;
break;
}
}
if (isRowEmpty)
{
sheet.Remove();
wbp.DeletePart(wsp);
}
}
这篇关于OpenXML如何检查工作表中的行是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文