110,533
社区成员
发帖
与我相关
我的任务
分享
private void querendan_Click(bool printModel, string printName, string hetongId, string hetongName, string hetongContect, string hetongTel, string hetongCode, string hetongType, string hetongSeal)
{
this.KillProcess("WINWORD");
string path = System.IO.Directory.GetCurrentDirectory();
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
string dotFilename = (hetongType == "1") ? @"\querenTem1.dot" : @"\querenTem2.dot";
object missing = System.Reflection.Missing.Value;
object templateFile = path + dotFilename;
app = new Microsoft.Office.Interop.Word.ApplicationClass();
doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
string printer = app.ActivePrinter;
app.ActivePrinter = (printName == "1") ? lstPrinter.Text : lstPrinter1.Text;
//声明书签数组
object[] oBookMark = new object[4];
oBookMark[0] = "hetongCode";
oBookMark[1] = "hetongContect";
oBookMark[2] = "hetongName";
oBookMark[3] = "hetongTel";
//赋值数据到书签的位置
doc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = hetongCode;
doc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = hetongContect;
doc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = hetongName;
doc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = hetongTel;
if (checkBox2.Checked == true && printName == "2")
{
string pngPath = label6.Text + @"\" + hetongId + ".png";
if (File.Exists(pngPath))
{
Random rd = new Random();
//要插入的图片文件
app.Selection.InlineShapes.AddPicture(pngPath, ref missing, ref missing, ref missing);
//将图片设置为四周环绕型
Microsoft.Office.Interop.Word.Shape cShape = doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
cShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTopBottom;
cShape.Left = rd.Next(350, 400);
cShape.Top = rd.Next(500, 550);// WdShapePosition.wdShapeCenter;居中
cShape.Rotation = rd.Next(100, 360);
//设置图片浮于文字之上 - 查阅WdWrapType的相关WdWrapType Enumeration
cShape.WrapFormat.Type = WdWrapType.wdWrapNone;
}
}
if (printModel || comboBox1.Text.Equals("打印"))
{
//打印
doc.PrintOut(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, ref missing, ref missing, ref missing);
//MessageBox.Show("确认单打印成功","提示");
}
else
{
//弹出保存文件对话框,保存生成的Word
app.Visible = false;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Word Document(*.doc)|*.doc";
sfd.DefaultExt = "Word Document(*.doc)|*.doc";
if (sfd.ShowDialog() == DialogResult.OK)
{
object filename = sfd.FileName;
doc.SaveAs(ref 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);
doc.Close(ref missing, ref missing, ref missing);
//关闭word
app.Quit(ref missing, ref missing, ref missing);
MessageBox.Show("确认单导出成功", "提示");
}
}
}
object linkToFile = false;
object saveWithDocument = true;
object range = app.Selection.Range;
object left = rd.Next(300, 350);
object top = rd.Next(500, 550);
float rotation = rd.Next(100, 360);
Shape cShape = app.ActiveDocument.Shapes.AddPicture(pngPath, ref linkToFile, ref saveWithDocument, ref left, ref top);
cShape.Rotation = rotation;