C# 操作Word 工作记录

1、目标:三种样式相对固定的Word表单,由表头和表格组成。其中部分内容存在系统性错误,需要修改。表单的正确信息以Excel形式存储。

2、方案:C#+Sqlite+?

2.1 先来简单的。准确读取Excel中正确信息,Sqlite比较顺手,直接将Excel转为Sqlite数据库,使用工具Sqlite expert

string dbfilepath = textBox2.Text;

SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder
{
    DataSource = dbfilepath
};

SQLiteConnection database = new SQLiteConnection
{
    ConnectionString = connstr.ToString()
};
 database.Open();

SQLiteCommand sql_command = new SQLiteCommand(Dwg_database);
sql_command = new SQLiteCommand("BEGIN TRANSACTION",  database);
sql_command.ExecuteNonQuery();


……
// 使用SQL语句查询并将结果存入DataSet
SQLiteDataAdapter dataadapter = new SQLiteDataAdapter();
DataSet ds = new DataSet();
sql_command.CommandText =
    string.Format("select a,b,c from db where a = '{0}'", pointName);
dataadapter.SelectCommand = sql_command;
dataadapter.Fill(ds, "bzd");//bzd 自行定义,方便后面取用

……
//关闭Sqlite数据库
  sql_command = new SQLiteCommand("COMMIT TRANSACTION", database);
  sql_command.ExecuteNonQuery();

2.2 操作word

2.2.1 目标表格重赋值,表格中文字替换

  //使用 Microsoft.Office.Interop.Word 操作word

   Word.Application wordApp = new Word.Application();
   object oFilePath = path;//文件路径
   object oReadOnly = false;
   object oMissing = Missing.Value;
   wordApp.Visible = false ; //打开本地文件可看
   Word.Document wordDoc = wordApp.Documents.Open(ref oFilePath, ref oMissing, ref         
       oReadOnly, ref oMissing, ref oMissing,
       ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
       ref oMissing);

  string context = wordDoc.Content.Text;//读取整篇文档的内容
  
  
//目标单元格赋值

  Word.Table nowTable = wordDoc.Tables[1];//注意[1]索引是从1开始的,不要从0开始喔
  nowTable.Cell(2, 4).Range.Text = "a"; //(2,4) 表格的第二行,第四列,再说一遍索引从 1 开始的
  nowTable.Cell(3, 4) .Range.Text = "b";

//表格中文字替换
targ = wordDoc.Paragraphs[i].Range.Text.Trim();
if (targ.Contains("种类"))
{
    wordApp.Selection.Find.ClearFormatting();
    wordApp.Selection.Find.Replacement.ClearFormatting();
    wordApp.Selection.Find.Text = targ;
    wordApp.Selection.Find.Replacement.Text = string.Format("种类:{0,-32}质料:{1}", 
            "a","b");
    object objReplace = WdReplace.wdReplaceAll;
    wordApp.Selection.Find.Execute(ref oMissing, ref oMissing, ref oMissing, 
           ref oMissing,  ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref objReplace, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing);
     
}

2.2.2 表头文字替换(失败)

string oldText = wordDoc.Paragraphs[i].Range.Text;
string text = oldText.Replace(" ", "");

if (text.Contains("标准普查登记表"))
{
    wordApp.Selection.Find.ClearFormatting();
    wordApp.Selection.Find.Text = oldText;
    wordApp.Selection.Find.Replacement.ClearFormatting();
    wordApp.Selection.Find.Replacement.Text = "标 志 普 查 登 记 表";
    object replaceAll = WdReplace.wdReplaceAll;
    wordApp.Selection.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}

表头中文字替换方法和表格中文字替换使用相同的方法,找到被替换内容,替换之,同样的方法在表格中好使但在替换表头文字时“wordApp.Selection.Find.Execute”返回结果为Ture,但保存后文档中表头未变化,期待高人点拨。

3、Npoi 尝试  以失败告终。

 //打开文件
 stream = File.OpenRead(path);
 FileStream fs = new FileStream(path, FileMode.Open);
 fs.Position = 0;
 XWPFDocument npoi_Doc = new XWPFDocument(fs);
 
 ……
 
 //目标表格重赋值 注意Npoi索引从0开始
 XWPFTable table = npoi_Doc.Tables[0];
 var para2 = table.GetRow(5).GetCell(1).Paragraphs[0];
 para2.ReplaceText(para2.ParagraphText, "a");
 
 ……
 
 //文字替换 
  foreach (var para in npoi_Doc.Paragraphs)
  {
      string oldText = para.ParagraphText; //获得文本
      string text = oldText.Replace(" ", "");

      if (text.Contains("标准普查登记表"))
      {
          para.ReplaceText(oldText, "标志普查登记表");
          break;
      }
      else if (text.Contains("标志普查登记表"))
      {
          break;
      }
  }
  ……
  
  //保存
    FileStream file = new FileStream(path, FileMode.Create);
    npoi_Doc.Write(file);
    file.Close();

 丝滑流畅,速度真快。

but  but  but

部分表格中图片没了,部分文件Word打不开了。 网上一搜,有同学指出Npoi操作Excel还行,操作Word就太勉强了。

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值