C#生成Word报表(插入文字、表格和图片)

回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档。时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/     

http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/  这个博客讲的用录制word宏的方法做参考很有用。感谢这位博友

静下心,认真做,不抱怨,总能把事解决。不扯了,直接进入正题

1.在开发word程序时一定要引用这个库:

using MSWord = Microsoft.Office.Interop.Word;

2.新建一个空word文档

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文件中的数据(给表格中写

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();

 

posted on 2016-02-01 23:01  2015在路上  阅读(7226)  评论(1编辑  收藏  举报

导航