需求:從上傳的一個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