您的位置:首页 > 运维架构

OpenXml 获取Excel文件指定工作簿中的表格部件和表格数量

2011-12-12 15:27 239 查看
代码:

using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace OpenXmlExcelGetTables
{
class Program
{
static void Main(string[] args)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Open("Tables.xlsx", true))
{
WorkbookPart wbPart = document.WorkbookPart;
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").FirstOrDefault();
if (theSheet == null)
{
throw new ArgumentException("sheetName");
}
WorksheetPart wsPart = wbPart.GetPartById(theSheet.Id) as WorksheetPart;
// get the tables in Excel
int count = wsPart.GetPartsOfType<TableDefinitionPart>().Count();
Console.WriteLine(count);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: