private void ExportToWps()
{
try
{
string strFileName = label14.Text + "-" + label15.Text;
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "doc";
saveDialog.Filter = "Word文件|*.doc";
saveDialog.FileName = strFileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
// Word.ApplicationClass oWordApp = new Word.ApplicationClass();//建立Word 对象,启动word程序
Word.Application oWordApp = new Word.Application();
if (oWordApp == null)
{
MessageBox.Show("word版本错误!", "error");
return;
}
object missing = System.Reflection.Missing.Value;
object oTemplate = System.Windows.Forms.Application.StartupPath + "\\Normal.dot";
Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);//新建word文档
oWordApp.Visible = false;//设置Word程序可见,如果为false 那么word不可见
//页面设置
oWordDoc.PageSetup.TopMargin = oWordApp.CentimetersToPoints(2.5f); //上
oWordDoc.PageSetup.BottomMargin = oWordApp.CentimetersToPoints(2f);//下
oWordDoc.PageSetup.LeftMargin = oWordApp.CentimetersToPoints(2.2f);//左
oWordDoc.PageSetup.RightMargin = oWordApp.CentimetersToPoints(2.2f);//右
////添加页眉 林总不需要
//oWordDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; //激活页眉的编辑
//oWordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; //设置对齐方式
//string headtext1 =PcaSettings.GetSettingString (101);
//oWordApp.Selection.Font.Name = "宋体"; //设置字体
//oWordApp.Selection.Font.Size = 10.5f;
//oWordApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
//oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone; //bu添加下划线
//oWordApp.Selection.TypeText(headtext1);
//oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
//添加页脚
string foottext1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter; //激活页脚的编辑
oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oWordApp.Selection.Font.Name = "仿宋_GB2312";
oWordApp.Selection.Font.Size = 8;
oWordApp.Selection.TypeText(foottext1);
//添加正文
oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//激活页面内容的编辑
oWordApp.Selection.Font.Name = "黑体";//标题使用黑体
oWordApp.Selection.Font.Scaling = 100;//视图里面的比例控制
//oWordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;
oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oWordApp.Selection.Font.Size = 16;
oWordApp.Selection.Font.Bold = 1;
oWordApp.Selection.TypeText(label14.Text);//主标题
oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
oWordApp.Selection.TypeText(label15.Text);//副标题
oWordApp.Selection.Font.Name = "宋体";
oWordApp.Selection.TypeParagraph();//另起一段
//oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
oWordApp.Selection.Font.Size = 11;
oWordApp.Selection.Font.Bold = 0;
#region 项不加粗
//oWordApp.Selection.TypeText(layoutControlItem3.Text + label6.Text); oWordApp.Selection.TypeText(", ");
//oWordApp.Selection.TypeText(layoutControlItem4.Text + label1.Text);
//oWordApp.Selection.TypeParagraph();//另起一段
//oWordApp.Selection.TypeText(layoutControlItem5.Text + label2.Text); oWordApp.Selection.TypeText(", ");
//oWordApp.Selection.TypeText(layoutControlItem6.Text + label3.Text);
//oWordApp.Selection.TypeParagraph();//另起一段
//oWordApp.Selection.TypeText(layoutControlItem7.Text + label4.Text); oWordApp.Selection.TypeText(", ");
//oWordApp.Selection.TypeText(layoutControlItem8.Text + label5.Text);
//oWordApp.Selection.TypeParagraph();//另起一段
//oWordApp.Selection.TypeText(layoutControlItem10.Text);
////oWordApp.Selection.TypeParagraph();//另起一段
//oWordApp.Selection.TypeText(label10.Text);
//SectDoc doc = GetDocument() as SectDoc;
//if (doc.FileCount > 1)
//{
// switch (doc.FileCount)
// {
// case 2:
// {
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(layoutControlItem12.Text);
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(label17.Text);
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);
// if (label12.Visible)
// {
// oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);
// }
// if (label13.Visible)
// {
// oWordApp.Selection.TypeText(layoutControlItem18.Text+label13.Text);
// }
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(label8.Text+";");
// oWordApp.Selection.TypeText(label9.Text);
// //oWordApp.Selection.TypeParagraph();//另起一段
// break;
// }
// case 3:
// {
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(layoutControlItem12.Text);
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(label17.Text);
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(layoutControlItem14.Text);
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(label19.Text);
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);
// //oWordApp.Selection.TypeParagraph();//另起一段
// break;
// }
// default:
// break;
// }
//}
//else
//{
// oWordApp.Selection.TypeParagraph();//另起一段
// oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text); oWordApp.Selection.TypeText(", ");
// oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text); oWordApp.Selection.TypeText(", ");
// oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text); oWordApp.Selection.TypeText(", ");
// oWordApp.Selection.TypeParagraph();//另起一段
//}
#endregion
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem3.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label6.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem4.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label1.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem5.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label2.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem6.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label3.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem7.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label4.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem8.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label5.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem10.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label10.Text);//不加粗的值
SectDoc doc = GetDocument() as SectDoc;
if (doc.FileCount > 1)
{
switch (doc.FileCount)
{
case 2:
{
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem12.Text);//加粗标题
//oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label17.Text);//不加粗的值
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
if (label12.Visible)
{
oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);
}
if (label13.Visible)
{
oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text);
}
//oWordApp.Selection.TypeParagraph();//另起一段
break;
}
case 3:
{
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.TypeText(layoutControlItem12.Text);
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.TypeText(label17.Text);
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.TypeText(layoutControlItem14.Text);
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.TypeText(label19.Text);
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);
//oWordApp.Selection.TypeParagraph();//另起一段
break;
}
default:
break;
}
}
else
{
oWordApp.Selection.TypeParagraph();//另起一段
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem16.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label12.Text);//不加粗的值
oWordApp.Selection.TypeText(", ");//各项之间间隔
oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem18.Text);//加粗标题
oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label13.Text);//不加粗的值
//oWordApp.Selection.TypeParagraph();//另起一段
}
oWordApp.Selection.Font.Size = 11.5f;
//表插入行
object start = oWordApp.Selection.Start;//在内容的最后插入表格
object end = oWordApp.Selection.End; ;
Word.Range tableLocation = oWordDoc.Range(ref start, ref end);
oWordDoc.Tables.Add(tableLocation, dataGridView1.RowCount + 1, dataGridView1.ColumnCount, ref missing, ref missing);
Word.Table newTable = oWordDoc.Tables[1];
//设置表格的格式
newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//内实体边框
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//外实体边框
newTable.AllowAutoFit = true;
newTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
//写入标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
newTable.Cell(1, i + 1).Range.Text = dataGridView1.Columns[i].HeaderText;
}
//写入数值
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
//电阻计算
if (dataGridView1.Rows[r].Cells[i].Value == null)
{
newTable.Cell(r + 2, i + 1).Range.Text = "";
}
else
{
newTable.Cell(r + 2, i + 1).Range.Text = dataGridView1.Rows[r].Cells[i].Value.ToString();
}
if (i == 6)
{
newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
else if (i == 7)
{
}
else
{
newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}
}
System.Windows.Forms.Application.DoEvents();
}
object wdUnits;
wdUnits = Word.WdUnits.wdLine;
object nCount = dataGridView1.RowCount + 1+1;
oWordApp.Selection.MoveDown(ref wdUnits, ref nCount, ref missing);
oWordApp.Selection.Font.Size = 12;
oWordApp.Selection.Font.Bold = 1;//防腐层和综合等级项加粗显示
oWordApp.Selection.TypeText(label8.Text); oWordApp.Selection.TypeText(", ");
oWordApp.Selection.TypeText(label9.Text);
string strfilename = saveFileName;
object filename = strfilename;
//保存文档为word2000格式
oWordDoc.SaveAs2000(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
MessageBox.Show(strFileName + "导出成功", "提示", MessageBoxButtons.OK);
//以下关闭Word程序
object nochanges = Word.WdSaveOptions.wdDoNotSaveChanges;
oWordApp.Quit(ref nochanges, ref missing, ref missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
---------------------
作者:leader0414
来源:CSDN
原文:https://blog.csdn.net/yanpengliumin/article/details/50344799
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件