回家前一個晚上,老闆臨時安排讓寫一個讀取txt文件的數據和多個圖片數據生成一個word文檔。時間給得過短只能是回家繼續加班,百度真是個好東西這裏引用一下我參考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/ 字體
http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/ 這個博客講的用錄製word宏的方法作參考頗有用。感謝這位博友this
靜下心,認真作,不抱怨,總能把事解決。不扯了,直接進入正題spa
1.在開發word程序時必定要引用這個庫:.net
using MSWord = Microsoft.Office.Interop.Word;orm
2.新建一個空word文檔blog
MSWord.Application wordApp = null;
MSWord.Document wordDoc = null; 圖片
wordApp = new MSWord.ApplicationClass();
wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);開發
3.給word文檔中添加文字文檔
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//設置字體居中
wordApp.Selection.Font.Size = 30;//設置字體大小
wordApp.Selection.Font.Bold = 2;//設置字體爲粗體
wordApp.Selection.TypeText(this.RouteTxtBox.Text + "分析報告");
wordApp.Selection.TypeParagraph(); //換行
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;//設置字體左對齊,之後的文字都是左對齊
wordApp.Selection.Font.Size = 20;
wordApp.Selection.Font.Bold = 2;
wordApp.Selection.TypeText("1.基本信息");
wordApp.Selection.TypeParagraph();
wordApp.Selection.Font.Size = 10;
wordApp.Selection.Font.Bold = 0;//設置字體正常,不加粗
wordApp.Selection.TypeText(" 名稱: " + this.RouteTxtBox.Text);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText(" 等級:" + this.KVtextBox.Text);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText(" 時間: " + this.TimeTxtBox.Text);
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText(" 標準:");
wordApp.Selection.TypeParagraph();
4.讀取txt文件中的數據(給表格中寫)get
OpenFileDialog opentxtDlg = new OpenFileDialog();
opentxtDlg.Title = "打開txt文檔";
opentxtDlg.Filter = "文本文件(*.txt)|*.txt";
opentxtDlg.ShowDialog();
string txtPath = opentxtDlg.FileName;
if (txtPath == "")
{
row = 0;
column = 0;
data = null;
return;
}
string[] lines = System.IO.File.ReadAllLines(txtPath, System.Text.Encoding.GetEncoding("gb2312"));
//string[] lines = System.IO.File.ReadAllLines(@"G:\testdata\1\1.txt", System.Text.Encoding.GetEncoding("gb2312"));
int txtrow = 0;
if (lines[lines.Length -1] == "")
{
txtrow = lines.Length - 1;
}
else
{
txtrow = lines.Length;
}
string[,] dat = new string[txtrow, 11];
int i = 0;
int j = 0;
foreach (string line in lines)
{
string[] str = line.Split(' ');
if (str.Length < 11)
{
break;
}
for (j = 0; j < str.Length; j++)
{
dat[i, j] = str[j];
}
i++;
}
row = i;
column = j;
data = dat;
5.在文字後面添加指定行列的表格
MSWord.Table table = wordDoc.Tables.Add(range, tableRow, tableColumn, ref nothing, ref nothing);
table.Borders.Enable = 1;//默認表格沒有邊框
//給表格中添加內容
//設置表頭
table.Cell(1, 1).Range.Text = "序號";
table.Cell(1, 1).Range.Bold = 2;
table.Cell(1, 1).Range.Font.Size = 8;
table.Cell(1, 2).Range.Text = "區間";
table.Cell(1, 2).Range.Bold = 2;
table.Cell(1, 2).Range.Font.Size = 8;
table.Cell(1, 3).Range.Text = "距離(m)";
table.Cell(1, 3).Range.Bold = 2;
table.Cell(1, 3).Range.Font.Size = 8;
table.Cell(1, 4).Range.Text = "點";
table.Cell(1, 4).Range.Bold = 2;
table.Cell(1, 4).Range.Font.Size = 8;
table.Cell(1, 5).Range.Text = "類型";
table.Cell(1, 5).Range.Bold = 2;
table.Cell(1, 5).Range.Font.Size = 8;
table.Cell(1, 6).Range.Text = "水平距離(m)";
table.Cell(1, 6).Range.Bold = 2;
table.Cell(1, 6).Range.Font.Size = 8;
table.Cell(1, 7).Range.Text = "垂直距離(m)";
table.Cell(1, 7).Range.Bold = 2;
table.Cell(1, 7).Range.Font.Size = 8;
table.Cell(1, 8).Range.Text = "距離(m)";
table.Cell(1, 8).Range.Bold = 2;
table.Cell(1, 8).Range.Font.Size = 8;
table.Cell(1, 9).Range.Text = "水平距離(m)";
table.Cell(1, 9).Range.Bold = 2;
table.Cell(1, 9).Range.Font.Size = 8;
table.Cell(1, 10).Range.Text = "距離(m)";
table.Cell(1, 10).Range.Bold = 2;
table.Cell(1, 10).Range.Font.Size = 8;
table.Cell(1, 11).Range.Text = "備註";
table.Cell(1, 11).Range.Bold = 2;
table.Cell(1, 11).Range.Font.Size = 8;
//**錯誤緣由——word中表格的行列是從1開始的,而不是從0開始的。**
//給表中添加信息(信息是從txt中讀取的)
for (int row = 2; row < tableRow + 1; row++)
{
for (int column = 1; column <= tableColumn; column++)
{
table.Cell(row, column).Range.Font.Size = 8;//設置表格中字體大小
table.Cell(row, column).Range.Bold = 0;//設置表格中字體不加粗
table.Cell(row, column).Range.Text = tableData[row - 2, column - 1].ToString();//給表格中添加信息
}
}
object wordLine = MSWord.WdUnits.wdLine;//換行
object count = tableRow + 1;//換行的數目
wordApp.Selection.MoveDown(ref wordLine, count, nothing); //向下移動count行
wordApp.Selection.TypeParagraph();
6.添加文字
object nothing = Type.Missing;
object EndOfDoc = "\\endofdoc";
object wordLine = MSWord.WdUnits.wdLine;
object count = tableRow+1;
wordApp.Selection.MoveDown(ref wordLine, count, nothing);
object what = MSWord.WdGoToItem.wdGoToBookmark;//定位到當前文檔末尾
wordApp.Selection.GoTo(what,nothing,nothing,EndOfDoc);
wordApp.Selection.TypeParagraph();
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
wordApp.Selection.Font.Size = 20;
wordApp.Selection.Font.Bold = 2;
wordApp.Selection.TypeText("3.詳情");
wordApp.Selection.TypeParagraph();
wordApp.Selection.Font.Size = 10;
wordApp.Selection.Font.Bold = 0;
wordApp.Selection.TypeText("實時");
wordApp.Selection.TypeParagraph();
7.添加表格
MSWord.Range range = wordDoc.Bookmarks.get_Item(ref EndOfDoc).Range;
MSWord.Table subtable = wordDoc.Tables.Add(range, 2, 11, ref nothing, ref nothing);
subtable.Borders.Enable = 1;//默認表格沒有邊框
subtable.Cell(1, 1).Range.Text = "序號";
subtable.Cell(1, 1).Range.Bold = 2;
subtable.Cell(1, 1).Range.Font.Size = 8;
subtable.Cell(1, 2).Range.Text = "區間";
subtable.Cell(1, 2).Range.Bold = 2;
subtable.Cell(1, 2).Range.Font.Size = 8;
subtable.Cell(1, 3).Range.Text = "距離(m)";
subtable.Cell(1, 3).Range.Bold = 2;
subtable.Cell(1, 3).Range.Font.Size = 8;
subtable.Cell(1, 4).Range.Text = "座標";
subtable.Cell(1, 4).Range.Bold = 2;
subtable.Cell(1, 4).Range.Font.Size = 8;
subtable.Cell(1, 5).Range.Text = "類型";
subtable.Cell(1, 5).Range.Bold = 2;
subtable.Cell(1, 5).Range.Font.Size = 8;
subtable.Cell(1, 6).Range.Text = "水平";
subtable.Cell(1, 6).Range.Bold = 2;
subtable.Cell(1, 6).Range.Font.Size = 8;
subtable.Cell(1, 7).Range.Text = "垂直";
subtable.Cell(1, 7).Range.Bold = 2;
subtable.Cell(1, 7).Range.Font.Size = 8;
subtable.Cell(1, 8).Range.Text = "淨空";
subtable.Cell(1, 8).Range.Bold = 2;
subtable.Cell(1, 8).Range.Font.Size = 8;
subtable.Cell(1, 9).Range.Text = "水平";
subtable.Cell(1, 9).Range.Bold = 2;
subtable.Cell(1, 9).Range.Font.Size = 8;
subtable.Cell(1, 10).Range.Text = "垂直";
subtable.Cell(1, 10).Range.Bold = 2;
subtable.Cell(1, 10).Range.Font.Size = 8;
subtable.Cell(1, 11).Range.Text = "備註";
subtable.Cell(1, 11).Range.Bold = 2;
subtable.Cell(1, 11).Range.Font.Size = 8;
for (int column = 1; column <= TableColumn; column++)
{
SubTable.Cell(2, column).Range.Text = LineData[row, column - 1].ToString();
SubTable.Cell(2, column).Range.Font.Size = 8;
}
8.在表格後面添加多個圖片
OpenFileDialog PictureDlg = new OpenFileDialog();
PictureDlg.Title = "打開圖片";
PictureDlg.Filter = "JPG圖片(*.jpg)|*.jpg|BMP圖片(*.bmp)|*.bmp|全部文件(*.*)|*.*";
PictureDlg.Multiselect = true;
PictureDlg.ShowDialog();
string[] picturePaths = PictureDlg.FileNames;
if (picturePaths.Length == 0)
{
picturePath = null;
pictureName = null;
return;
}
string[] pictureNames = new string[picturePaths.Length];
for (int index = 0; index < picturePaths.Length; index++)
{
pictureNames[index] = picturePaths[index].Substring(picturePaths[index].LastIndexOf("\\") + 1);
}
picturePath = picturePaths;
pictureName = pictureNames;
//插入圖片
object wordLine = MSWord.WdUnits.wdLine;
object count = 3;
wordApp.Selection.MoveDown(ref wordLine, count, nothing);
wordApp.Selection.TypeParagraph();//插入段落 80
object LinkOfFile = false;
object SaveDocument = true;
//object range = wordApp.Selection.Range;
object range3 = wordDoc.Bookmarks.get_Item(ref EndOfDoc).Range;
wordDoc.InlineShapes.AddPicture(picturePath, ref LinkOfFile, ref SaveDocument, ref range3);
object wordcharacter = MSWord.WdUnits.wdCharacter; object count1 = 1; wordApp.Selection.MoveRight(ref wordcharacter, count1, nothing); wordApp.Selection.TypeParagraph();