讀取word指定表格內容?
1、注意Office版本,一般office2016環境下寫的東西,office2013環境下是運行不了的。(據說可以有第三方庫支持,以后慢慢研究)
2、注意中斷程序號一定要注銷計算機,否則docx文件老是提示被占用。
3、注意try catch finally一定得加上。
4、多余符號用正則表達排除
/// <summary>
/// 獲取word文件的文本內容
/// </summary>
/// <param name="docFileName"></param>
/// <returns></returns>
private string DocToExcel2(string docFileName)
{
//實例化COM
Word.ApplicationClass app = null;
Word.Document wd = null;
object nullobj = System.Reflection.Missing.Value;
object fileobj = docFileName;
string context = string.Empty;
try
{
app = new Word.ApplicationClass();
wd = app.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj);
//取得doc文件中的文本內容
foreach (Word.Table table in wd.Tables)
{
//遍歷每一行去處理
for (int k = 0; k < table.Rows.Count; k++)
{
string firstContent = table.Cell(k, 1).Range.Text;
//圖上編號
Regex regTSBH1 = new Regex(@"^圖上編號.+");
//Match mcMPH1 = regMPH1.Match();
if (regTSBH1.IsMatch(firstContent))
{
Regex regTSBH2 = new Regex(@"^([^\t\v\s]+).+");
Match mcTSBH2 = regTSBH2.Match(table.Cell(k, 2).Range.Text.Trim());
context += /*mcMPH1.Groups[1].Value.Trim() + "," + */mcTSBH2.Groups[1].Value.Trim() + ",";
}
//門牌號
Regex regMPH1 = new Regex(@"^門牌號.+");
//Match mcMPH1 = regMPH1.Match();
if (regMPH1.IsMatch(firstContent))
{
Regex regMPH2 = new Regex(@"^([^\t\v\s]+).+");
Match mcMPH2 = regMPH2.Match(table.Cell(k, 2).Range.Text.Trim());
context += /*mcMPH1.Groups[1].Value.Trim() + "," + */mcMPH2.Groups[1].Value.Trim() + ",";
}
//戶主
Regex regHZ1 = new Regex(@"^戶主.+");
//Match mcHZ1 = regHZ1.Match(table.Cell(5, 1).Range.Text.Trim());
if (regHZ1.IsMatch(firstContent))
{
Regex regHZ2 = new Regex(@"^^([^\t\v\s]+).+");
Match mcHZ2 = regHZ2.Match(table.Cell(k, 2).Range.Text.Trim());
context += /*mcHZ1.Groups[1].Value.Trim() + "," + */mcHZ2.Groups[1].Value.Trim() + ",";
}
//電話
Regex regDH1 = new Regex(@"^電話.+");
//Match mcDH1 = regDH1.Match(table.Cell(6, 1).Range.Text.Trim());
if (regDH1.IsMatch(firstContent))
{
Regex regDH2 = new Regex(@"^([^\t\v\s]+).+");
Match mcDH2 = regDH2.Match(table.Cell(k, 2).Range.Text.Trim());
context += /*mcDH1.Groups[1].Value.Trim() + "," + */mcDH2.Groups[1].Value.Trim() + ",";
}
//樓層數
Regex regLCS1 = new Regex(@"^樓層數.+");
//Match mcLCS1 = regLCS1.Match(table.Cell(7, 1).Range.Text.Trim());
if (regLCS1.IsMatch(firstContent))
{
Regex regLCS2 = new Regex(@"^([^\t\v\s]+).+");
Match mcLCS2 = regLCS2.Match(table.Cell(k, 2).Range.Text.Trim());
context += /*mcLCS1.Groups[1].Value.Trim() + "," + */mcLCS2.Groups[1].Value.Trim() + ",";
context += /*mcLCS1.Groups[1].Value.Trim() + "," + */docFileName + "\r\n";
}
//文件名,用于排錯
//Regex regLCS1 = new Regex(@"^([\u4e00-\u9fa5]+).+");
//Match mcLCS1 = regLCS1.Match(table.Cell(7, 1).Range.Text.Trim());
//if((k % 7 == 0)&&(k != 0))
//{
// context += /*mcLCS1.Groups[1].Value.Trim() + "," + */docFileName + "\r\n";
//}
}
}
}
catch (Exception error)
{
MessageBox.Show("Error:" + error.Message);
}
finally
{
//關閉文件
wd.Close(ref nullobj, ref nullobj, ref nullobj);
//關閉COM
app.Quit(ref nullobj, ref nullobj, ref nullobj);
}
//返回文本內容
return context;
}