1.从datagirdview生成的表格总是新打开一个文档保存,而不是和内容一块儿保存在生成的新文档保存到本地。
2.书签替换内容可以实现,请教datagridview生成的表格怎么让他显示在报告的后面。
```c#
public bool ExportDataGridview(DataGridView dgv, bool isShowWord, string tempPath, string fileName)
{
wordApp = new Microsoft.Office.Interop.Word.Application();
//模板文件
string TemplateFile = tempPath;
//生成的具有模板样式的新文件
string FileName = fileName;
//模板文件拷贝到新文件
File.Copy(TemplateFile, FileName);
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object Obj_FileName = FileName;
object Visible = false;
object missing = System.Reflection.Missing.Value;
wordDoc = wordApp.Documents.Open(ref Obj_FileName, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
//wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;//显示警报
wordApp.Visible = true;
Microsoft.Office.Interop.Word.Table mytable;
Microsoft.Office.Interop.Word.Selection mysel;
Object myobj;
if (dgv.Rows.Count == 0)
return false;
//建立Word对象
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
myobj = System.Reflection.Missing.Value;
doc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
word.Visible = true;
doc.Select();
mysel = word.Selection;
//将数据生成Word表格文件
mytable = doc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj);
mytable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
mytable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
mytable.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParag
raphCenter;//表格文本居中
//输出列标题数据
for (int i = 0; i < dgv.ColumnCount; i++)
{
mytable.Cell(1, i+1).Range.InsertAfter(dgv.Columns[i].HeaderText);
}
mytable.Cell(1, 3).Merge(mytable.Cell(1, 4));
mytable.Cell(1, 1).Range.Font.Bold = 1;//加粗
mytable.Cell(1, 1).Range.Font.Name = "黑体";//字体
mytable.Cell(1, 1).Range.Font.Size = 12; //字体大小
mytable.Cell(1, 2).Range.Font.Bold = 1;//加粗
mytable.Cell(1, 2).Range.Font.Name = "黑体";//字体
mytable.Cell(1, 2).Range.Font.Size = 12; //字体大小
mytable.Cell(1, 3).Range.Font.Bold = 1;//加粗
mytable.Cell(1, 3).Range.Font.Name = "黑体";//字体
mytable.Cell(1, 3).Range.Font.Size = 12; //字体大小
//输出控件中的记录
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
mytable.Cell(i+2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString());
}
}
return true;
}
public void SaveDocument(string filePath)
{
object fileName = filePath;
object format = WdSaveFormat.wdFormatDocument;//保存格式
object miss = System.Reflection.Missing.Value;
wordDoc.SaveAs(ref fileName, ref format, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
//输出完毕后关闭doc对象
object IsSave = true;
wordDoc.Close(ref IsSave, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);
}
//关闭当前文件
public bool CloseDocument()
{
try
{
object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
wordDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
wordDoc = null;
return true;
}
catch (Exception)
{
return false;
}
}
private void Button1_Click(object sender, EventArgs e)
{
string tempPath = System.Windows.Forms.Application.StartupPath + "\\唯一.dot";
//生成的具有模板样式的新文件
string fileName = @"C:\Users\Administrator\Desktop\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
myReport.ExportDataGridview(dataGridView1,true,tempPath, fileName);
string reportname = richTextBox1.Text;
string reportname1 = richTextBox2.Text;
string reportname3 = Head.Text;
string reportname4 = DateTime.Now.ToString("yyyy年-MM月-dd日");
myReport.Insertxt("Pro1", reportname);
myReport.Insertxt1("Pro2", reportname1);
myReport.Insertxt3("Heads", reportname3);
myReport.Insertxt4("Timard",reportname4);
}