发现有些电脑中使用NPOI读取word文档会出现 Wrong Local header signature: 0xE011CFD0 的报错
于是就使用Spire.Doc 来获取word文档中的表格数据
代码如下
private string[][] GetDocX2(int index, string Path)
{
Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile(Path);
Spire.Doc.Table table = doc.Sections[0].Tables[index] as Table;
string[][] tableData = new string[table.Rows.Count][];
//遍历表格内容
for (int i = 0; i < table.Rows.Count; i++)
{
var cellsindex = table.Rows[i].Cells.Count;
tableData[i] = new string[cellsindex];
for (int j = 0; j < cellsindex; j++)
{
TableCell cell = table.Rows[i].Cells[j];
string txt = "";
foreach (Paragraph paragraph in cell.Paragraphs)
{
txt += paragraph.Text;
}
tableData[i][j] = txt;
}
}
return tableData;
}
index 是word文档中的表格下标数,可以使用doc.Sections[0].Tables.Count 来获取文档中的表格数量