C#操作Word(開啟建立,選擇複製,寫入資料,儲存關閉)
阿新 • • 發佈:2019-02-16
(1)必要變數(欄位)定義
private object Nothing = Missing.Value;//預設值
private object IsReadOnly = false;//不僅僅可讀
private MSWord._Application wordApp;//Word應用程式
private MSWord._Document wordDoc;//Word文件
private int tableCount;//Word表格數目(完整的)
注:前面4個是操作Word時,必須定義的欄位;最後一個適應本程式
(2)讀取txt資料
//原始資料讀取
private List<string> TextData = new List<string>();//文字資料(去除41000……)
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "E:\\";
openFileDialog1.Filter = "GSI(*.gsi)|*.gsi";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
TextData = File.ReadAllLines(openFileDialog1.FileName).Where(s => (s[0 ] != '4') && (s != "")).ToList();
}
tableCount = (int)(TextData.Count / 11 * 2 / 7);
MessageBox.Show("共讀取" + TextData.Count / 11 + "測段的資料!");
}
(3)開啟原本的Word文件,並複製其中的表格
//載入水準簿(並進行表格複製)
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
openFileDialog2.InitialDirectory = "e:\\" ;
openFileDialog2.Filter = "文件(*.docx)|*.docx|(*.doc)|*.doc";
if(openFileDialog2.ShowDialog()==DialogResult.OK)
{
try
{
foreach (Process item in Process.GetProcessesByName("WINWORD"))
{ item.Kill(); }//如存在開啟的Word則先關閉(一個應用程式例項就是程序)
object filePath=openFileDialog2.FileName;
wordApp = new MSWord.Application(); //應用程式例項化
//wordApp.Visible = true;//可見(用的是wordApp顯示)
wordDoc = wordApp.Documents.Open(ref filePath, ref Nothing, ref IsReadOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.Tables[1].Select();//選擇與複製:文件用Sections[節]/應用程式用Selection[選擇]
wordApp.Selection.Copy();
//////還可以是:(會複製文件第一部分所有內容--包括標題)
//wordDoc.Select();
//wordDoc.Sections[1].Range.Copy();
//wordDoc.Sections[1].Range.InsertBreak(ref myType); //以頁為單元,表格連續(只複製表格)
//wordDoc.Sections[1].Range.PasteAndFormat(MSWord.WdRecoveryType.wdPasteDefaul);//複製語句:文件形式
object myType = MSWord.WdBreakType.wdSectionBreakContinuous;//換行符
object myUnit = MSWord.WdUnits.wdStory;
object pBreak = (int)MSWord.WdBreakType.wdPageBreak;//下一頁(上一頁最後一行空,否則跳入下下頁)
//object start = wordApp.ActiveWindow.Selection.End;//定位文件末尾
//object end = wordApp.ActiveWindow.Selection.End;
//MSWord.Range rng = wordDoc.Range(ref start, ref end);
//rng.Select();
//rng.Paste();
for (int i = 0; i < tableCount-1;i++ )//原本手簿已存在一個,則複製減1個
{
wordApp.Selection.EndKey(ref myUnit, ref Nothing);
wordApp.Selection.InsertBreak(ref pBreak);
wordApp.Selection.PasteAndFormat(MSWord.WdRecoveryType.wdPasteDefault);//複製語句:應用程式形式
//wordApp.Selection.Paste();//複製語句:簡單形式
}
MessageBox.Show("手簿載入完成!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
(4)將txt資料寫入變化後的Word文件並儲存
//原始資料:TextData中
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
try
{
#region 文件頭
object bm1 = "雲量";
wordDoc.Bookmarks.get_Item(ref bm1).Range.Text = "多雲";
object bm2 = "風向風速";
wordDoc.Bookmarks.get_Item(ref bm2).Range.Text = "無風";
object bm3 = "天氣";
wordDoc.Bookmarks.get_Item(ref bm3).Range.Text = "晴朗";
#endregion
List<string> Data = TextData.Where(s => s.ToCharArray().Count() > 35).ToList();//全部都是5行5行的
int tabturn = 0;//表格計數
foreach(MSWord.Table table in wordDoc.Tables)
{
for (int i = 1; i < 8; i++)
{
List<string> tempData = new List<string>();
for (int j = 35 * tabturn + (i - 1) * 5; j < 35 * tabturn + i * 5; j++)
{
tempData.Add(Data[j]);
}
#region 文件表格賦值
table.Cell(4 * i + 1, 1).Range.Text = i + "測站";
table.Cell(4 * i + 1, 5).Range.Text = (double.Parse(tempData[0].Trim().Split(' ')[2].Substring(6, 9)) / 100.0).ToString();//後尺基本(mm)
table.Cell(4 * i + 2, 5).Range.Text = (double.Parse(tempData[1].Trim().Split(' ')[2].Substring(6, 9)) / 100.0).ToString();//前尺基本(mm)
table.Cell(4 * i + 2, 6).Range.Text = (double.Parse(tempData[2].Trim().Split(' ')[2].Substring(6, 9)) / 100.0).ToString();//前尺輔助(mm)
table.Cell(4 * i + 1, 6).Range.Text = (double.Parse(tempData[3].Trim().Split(' ')[2].Substring(6, 9)) / 100.0).ToString();//後尺輔助(mm)
table.Cell(4 * i + 3, 2).Range.Text = ((double.Parse(tempData[0].Trim().Split(' ')[1].Substring(6, 9)) / 1e2 + double.Parse(tempData[3].Trim().Split(' ')[1].Substring(6, 9)) / 1e2) / 2).ToString();//後視距離(mm)
table.Cell(4 * i + 3, 3).Range.Text = ((double.Parse(tempData[1].Trim().Split(' ')[1].Substring(6, 9)) / 1e2 + double.Parse(tempData[2].Trim().Split(' ')[1].Substring(6, 9)) / 1e2) / 2).ToString();//前視距離(mm)
table.Cell(4 * i + 4, 2).Range.Text = ((int)(((double.Parse(tempData[0].Trim().Split(' ')[1].Substring(6, 9)) / 1e5 + double.Parse(tempData[3].Trim().Split(' ')[1].Substring(6, 9)) / 1e5) / 2 - (double.Parse(tempData[1].Trim().Split(' ')[1].Substring(6, 9)) / 1e5 + double.Parse(tempData[2].Trim().Split(' ')[1].Substring(6, 9)) / 1e5) / 2) * 10000 + 0.5) / 10000.0).ToString();//後前視距離差(m)
table.Cell(4 * i + 4, 3).Range.Text = (double.Parse(tempData[4].Trim().Split(' ')[3].Substring(6, 9)) / 1e5).ToString();//前後視距差累積(m)
table.Cell(4 * i + 1, 7).Range.Text = ((int)((double.Parse(tempData[0].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - double.Parse(tempData[3].Trim().Split(' ')[2].Substring(6, 9)) / 100.0) * 1000 + 0.5) / 1000.0).ToString();//基+K-輔(mm)
table.Cell(4 * i + 2, 7).Range.Text = ((int)((double.Parse(tempData[1].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - double.Parse(tempData[2].Trim().Split(' ')[2].Substring(6, 9)) / 100.0) * 1000 + 0.5) / 1000.0).ToString();//基+K-輔(mm)
table.Cell(4 * i + 3, 5).Range.Text = ((int)((double.Parse(tempData[0].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - double.Parse(tempData[1].Trim().Split(' ')[2].Substring(6, 9)) / 100.0) * 10000 + 0.5) / 10000.0).ToString(); //後-前(基本mm)
table.Cell(4 * i + 3, 6).Range.Text = ((int)((double.Parse(tempData[3].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - double.Parse(tempData[2].Trim().Split(' ')[2].Substring(6, 9)) / 100.0) * 10000 + 0.5) / 10000.0).ToString(); //後-前(輔助mm)
table.Cell(4 * i + 3, 7).Range.Text = ((int)((double.Parse(tempData[0].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - double.Parse(tempData[3].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - (double.Parse(tempData[1].Trim().Split(' ')[2].Substring(6, 9)) / 100.0 - double.Parse(tempData[2].Trim().Split(' ')[2].Substring(6, 9)) / 100.0)) * 1000 + 0.5) / 1000.0).ToString(); //檢核
table.Cell(4 * i + 4, 6).Range.Text = (double.Parse(tempData[4].Trim().Split(' ')[5].Substring(6, 9)) / 1e5).ToString();//高差
#endregion
}
tabturn += 1;
}
//文件另存為並退出
saveFileDialog1.Filter = "文件(*.docx)|*.docx|(*.doc)|*.doc";
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
object filePath = saveFileDialog1.FileName;
object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;//docx
//object format = MSWord.WdSaveFormat.wdFormatDocument;//doc
wordDoc.SaveAs(ref filePath, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
((MSWord._Document)wordDoc).Close(ref Nothing, ref Nothing, ref Nothing);
((MSWord._Application)wordApp).Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (wordApp != null)
{
Marshal.FinalReleaseComObject(wordApp);
wordApp = null;
}
if (wordDoc != null)
{
Marshal.FinalReleaseComObject(wordDoc);
wordDoc = null;
}
}
}
注:當向Word表格中輸入資料時,迴圈處程式碼較多,但本質上並不複雜。
(5)部分txt文字資料(萊卡數字水準儀資料)
410001+?......2
110002+000000A1 83..28+00000000
110003+000000A1 32...8+01471340 331.08+00096547 390...+00000002 391.08+00000000 71....+00000001
110004+00000001 32...8+01457115 332.08+00144543 390...+00000002 391.08+00000005
110005+00000001 32...8+01456595 336.08+00144553 390...+00000002 391.08+00000002
110006+000000A1 32...8+01470969 335.08+00096549 390...+00000002 391.08+00000001
110007+00000001 571.08+00000008 572.08+00000008 573..8+00014300 574..8+02928010 83..08-00048000
110008+00000001 32...8+01616093 331.08+00143847 390...+00000002 391.08+00000001
110009+00000002 32...8+01626730 332.08+00136480 390...+00000002 391.08+00000000
110010+00000002 32...8+01626589 336.08+00136477 390...+00000002 391.08+00000001
110011+00000001 32...8+01616202 335.08+00143831 390...+00000002 391.08+00000024
110012+00000002 571.08+00000013 572.08+00000021 573..8+00003788 574..8+06170816 83..08-00040640
410013+?......2
110014+000000A1 83..08+00000000
110015+000000A1 32...8+01625880 331.08+00136485 390...+00000002 391.08+00000003
110016+00000001 32...8+01617791 332.08+00143837 390...+00000002 391.08+00000004
110017+00000001 32...8+01617711 336.08+00143834 390...+00000002 391.08+00000003
110018+000000A1 32...8+01624988 335.08+00136499 390...+00000002 391.08+00000002
110019+00000001 571.08-00000017 572.08-00000017 573..8+00007683 574..8+03243185 83..08-00007344
110020+00000001 32...8+01460178 331.08+00144567 390...+00000002 391.08+00000001
110021+00000002 32...8+01469076 332.08+00096601 390...+00000002 391.08+00000000
110022+00000002 32...8+01469139 336.08+00096599 390...+00000002 391.08+00000002
110023+00000001 32...8+01460232 335.08+00144565 390...+00000002 391.08+00000001
110024+00000002 571.08-00000001 572.08-00000018 573..8-00001220 574..8+06172497 83..08+00040622