需求:从上传的一个word文件中读取其中表格中的数据,并写入数据库
实现方法:
1、需要引用一个类库
2、命名空间需要引用
using Word= Microsoft.Office.Interop.Word;
3、
public void AddSug(string fileFullPath, string fileName)
{
object oFileName = fileFullPath;//文件路径
object oReadOnly = true;
object oMissing = System.Reflection.Missing.Value;
Word.Application oWord;
Word.Document oDoc;
oWord = new Word.Application();
// oWord.Visible = true; 打开本地文件可看
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
{
Word.Table nowTable = oDoc.Tables[tablePos];//tablePos表示第几个表
//string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);
for (int rowPos = 2; rowPos <= nowTable.Rows.Count; rowPos++)//rowPos 表示第几行
{
Bdyh.Custom.Domain.Entities.LegSuggest leg = new Bdyh.Custom.Domain.Entities.LegSuggest();
//for (int columPos = 2; columPos <= nowTable.Columns.Count; columPos++)
//{
// tableMessage = nowTable.Cell(rowPos, columPos).Range.Text;
// // tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a
//} //这种写法适用于读取全部表格数据
//建议名称
var S_ProName = nowTable.Cell(rowPos, 2).Range.Text; //2表示这行第二个单元格
leg.S_ProName = S_ProName.Substring(0, S_ProName.Length - 2);
//起草单位
leg.QCDW = HtmlHelps.GetStr(nowTable.Cell(rowPos, 3).Range.Text, 4);
//建议主体
leg.S_Name = HtmlHelps.GetStrId(nowTable.Cell(rowPos, 3).Range.Text, 4);
}
}
}
可能报的错:
1、检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005
解决方法结合以下几个链接:
https://www.jb51.net/article/40077.htm
https://bbs.csdn.net/topics/390586357
权限不够,试试把ASP.NET和IUSER这类用户加入到Administrators组中。(重启生效)
方法:右击我的电脑--管理--系统管理--本地用户和组--右击“ASP.NET”--属性--隶属于--添加--输入“Administrators”--确定
如果对文档进行修改后再读取,会重复出现上述错误,我试过重启电脑就好,具体原因不晓得。
另外有一种不需要引用office的方法,即:Aspose.Words
命名空间需要引用:using Aspose.Words; using Aspose.Words.Tables;
Aspose.Words.Document doc = new Aspose.Words.Document(fileFullPath);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
//获取标题
string Title = doc.Range.Bookmarks[0].Text;
// 遍历表
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);//正式件
Table table2 = (Table)doc.GetChild(NodeType.Table, 1, true);
for (int i = 1; i < table.Count; i++)
{
Bdyh.Custom.Domain.Entities.LegSuggest leg = new Bdyh.Custom.Domain.Entities.LegSuggest();
if (i != 0)
{
Row clonedRow = (Row)table.Rows[i]; //这是一行的内容
//foreach (Cell cell in clonedRow.Cells)
//{
// string cellText = cell.ToString(SaveFormat.Text).Trim();
//}
//建议名称
leg.S_ProName = clonedRow.Cells[1].ToString(SaveFormat.Text).Trim();
//起草单位
leg.QCDW = HtmlHelps.GetStr(clonedRow.Cells[2].ToString(SaveFormat.Text).Trim(), 4);
//建议主体
leg.S_Name = HtmlHelps.GetStrId(clonedRow.Cells[2].ToString(SaveFormat.Text).Trim(), 4);
}
}
————————————————
版权声明:本文为CSDN博主「終99」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_28548963/java/article/details/83013038