///
///
public void CreateWordFile(DataRow item)
{
try
{
MultiFormatWriter multiWriter = new MultiFormatWriter();
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
BarcodeWriter bw = new BarcodeWriter();
bw.Format = BarcodeFormat.CODE_128;
bw.Options = options;
object Nothing = System.Reflection.Missing.Value;
//定义该插入图片是否为外部链接
object linkToFile = true;
//定义插入图片是否随word文档一起保存
object saveWithDocument = true;
WordApp = new Word.Application();
WordDoc = new Word.Document();
WordApp.Visible = false;
WordApp.PrintPreview = false;
string filename = System.Windows.Forms.Application.StartupPath + [\\\\\*\*\*\*\*\*\\\\Word.dot](//\\******\\Word.dot);
try
{
WordDoc = WordApp.Documents.Add(filename);
WordDoc = WordApp.ActiveDocument;
}
catch (Exception ex)
{
throw (ex);
}
WordDoc.Tables\[\].Cell(, ).Range.Text = HospitalName;
int x = ;
//Table2 x = 1
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["CORP\_DEPT\_NAME"\].ToString().Trim();
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["MEMO"\].ToString().Trim();
options.Width = ;
options.Height = ;
options.Margin = ;
options.PureBarcode = false;
Image BarCode = bw.Write(item\["REGISTER\_NO"\].ToString().Trim());
Clipboard.SetDataObject(BarCode);
WordDoc.Tables\[x\].Cell(, ).Range.Paste();
BarCode.Dispose();
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["PATIENT\_NAME"\].ToString().Trim();
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["BIRTHDAY"\].ToString().Trim();
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["CHART\_NO"\].ToString().Trim();
if (item\["CORPORATION\_NAME"\].ToString().Length > )
{
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["CORPORATION\_NAME"\].ToString().Trim().Substring(, );
}
else
{
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["CORPORATION\_NAME"\].ToString().Trim();
}
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["BOOKING\_DATE"\].ToString().Trim();
if (item\["ADDRESS"\].ToString().Length > )
{
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["ADDRESS"\].ToString().Trim().Substring(, );
}
else
{
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["ADDRESS"\].ToString().Trim();
}
WordDoc.Tables\[x\].Cell(, ).Range.Text = item\["PHONE\_NUMBER"\].ToString().Trim();
if (item\["PHOTO"\].ToString() != "")
{
SaveFileDialog sflg = new SaveFileDialog();
MemoryStream ms = new MemoryStream((byte\[\])item\["PHOTO"\]);//把照片读到MemoryStream里
Image imageBlob = Image.FromStream(ms, true);//用流创建Image
imageBlob.Save(System.Windows.Forms.Application.StartupPath + "\\\\" + item\["REGISTER\_NO"\].ToString() + ".jpg");
////插入图片
WordDoc.Tables\[x\].Cell(, ).Range.Text = "";
WordDoc.Tables\[x\].Cell(, ).Select();
object range = WordApp.Selection.Range;
Word.InlineShape shape = WordApp.ActiveDocument.InlineShapes.AddPicture(System.Windows.Forms.Application.StartupPath + "\\\\" + item\["REGISTER\_NO"\].ToString() + ".jpg", ref linkToFile, ref saveWithDocument, ref range);
shape.Width = ;//图片宽度
shape.Height = ;//图片高度
System.IO.FileInfo file = new System.IO.FileInfo(System.Windows.Forms.Application.StartupPath + "\\\\" + item\["REGISTER\_NO"\].ToString() + ".jpg");
if (file.Exists)
{
file.Delete();
}
imageBlob.Dispose();
}
DataTable dtHMS\_REGISTER = hmsService.QueryHmsRequestDatafor0131(item\["REGISTER\_NO"\].ToString().Trim(), strHavePrint);
dtHMS\_REGISTER.DefaultView.Sort = "STATION\_ID";
System.Data.DataTable dttemp = new System.Data.DataTable();
DataView tempDv = dtHMS\_REGISTER.DefaultView;
dtHMS\_REGISTER = tempDv.ToTable();
dttemp = tempDv.ToTable(true, new\[\] { "STATION\_NAME", "FLOW\_SHEET\_MEMO" });
bool blMerge = false;
Table table = null;
int iRow = ;
WordDoc.Content.Tables\[x\].Rows.Add(ref Nothing);
WordDoc.Tables\[x\].Cell(, ).Range.Text = "(1)登记报到";
for (int i = ; i < dttemp.Rows.Count; i++, iRow++)
{
string strMemo = "";
DataRow\[\] dr = dtHMS\_REGISTER.Select("STATION\_NAME = '" + dttemp.Rows\[i\]\["STATION\_NAME"\].ToString().Trim() + "'");
if (dr.Length > )
{
//创建新行
if (i > )
{
WordDoc.Content.Tables\[x\].Rows.Add(ref Nothing);
//拆分以合并行
if (blMerge)
{
WordDoc.Content.Tables\[x\].Cell(iRow, ).Merge(WordDoc.Tables\[x\].Cell(iRow, ));
WordDoc.Content.Tables\[x\].Cell(iRow, ).Split(, );
WordDoc.Content.Tables\[x\].Cell(iRow, ).Width = ;
WordDoc.Content.Tables\[x\].Cell(iRow, ).Width = 70.5f;
WordDoc.Content.Tables\[x\].Cell(iRow, ).Width = ;
blMerge = false;
}
}
WordDoc.Tables\[x\].Cell(iRow, ).Range.Text = dr\[\]\["STATION\_NAME"\].ToString().Trim();
int iNewRow = ;
int iCol = ;
int m = ;
string strModType = "";
string strCollingNo = "";
for (int j = ; j < dr.Length; j++)
{
#region 检查站类别(STATION\_TYPE\_CODE)=’1’(表示输入)
if (dr\[j\]\["STATION\_TYPE\_CODE"\].ToString().Trim() == "")
{
#region 体检项目基本档(HMS\_EXAM\_ITEM)中指引单打印标志(HMS\_EXAM\_ITEM.FLOW\_SHEET\_FLAG)=’Y’时
if (dr\[j\]\["FLOW\_SHEET\_FLAG"\].ToString().Trim() == "Y")
{
//FLOW\_SHEET\_NAME
if (j == )
{
WordDoc.Tables\[x\].Cell(iRow, ).Select();
table = CreatTable();
}
else
{
if (j % == )
{
table.Rows.Add(ref Nothing);
iNewRow++;
}
}
iCol = j % + ;
table.Cell(iNewRow, iCol).Range.Text = "□ " + dr\[j\]\["FLOW\_SHEET\_NAME"\].ToString().Trim();
table.Cell(iNewRow, iCol).Range.Font.Name = "微软雅黑";
table.Cell(iNewRow, iCol).Range.Font.Size = 10.5F;
}
else
{
continue;
}
if (dr\[j\]\["EXAM\_MEMO"\].ToString().Trim() != "" && strMemo.IndexOf(dr\[j\]\["EXAM\_MEMO"\].ToString().Trim() + ",") < )
{
strMemo += dr\[j\]\["EXAM\_MEMO"\].ToString().Trim() + ",";
}
#endregion
}
else if (dr\[j\]\["STATION\_TYPE\_CODE"\].ToString().Trim() == "" || (dr\[j\]\["STATION\_TYPE\_CODE"\].ToString().Trim() == ""))
{
#region 检核报告主档(HMS\_REPORT)中申请单号(OBSERVATION\_NO)有值时
if (dr\[j\]\["OBSERVATION\_NO"\].ToString().Trim() != "" && dr\[j\]\["FLOW\_FLAG"\].ToString().Trim() == "Y")
{
if (dr\[j\]\["FLOW\_TYPE\_CODE"\].ToString().Trim() == "")
{
if (j == )
{
WordDoc.Tables\[x\].Cell(iRow, ).Select();
table = CreatTable();
}
else
{
if (j % == )
{
table.Rows.Add(ref Nothing);
iNewRow++;
}
}
iCol = j % + ;
table.Cell(iNewRow, iCol).Range.Text = "□ " + dr\[j\]\["PRINT\_TAG\_NAME"\].ToString().Trim();
table.Cell(iNewRow, iCol).Range.Font.Name = "微软雅黑";
table.Cell(iNewRow, iCol).Range.Font.Size = 10.5F;
}
else if (dr\[j\]\["FLOW\_TYPE\_CODE"\].ToString().Trim() == "")
{
if (j == )
{
WordDoc.Tables\[x\].Cell(iRow, ).Select();
table = CreatTable();
m = j;
}
else
{
m = j \* ;
table.Rows.Add(ref Nothing);
}
if (Convert.ToInt32("" + dr\[j\]\["CALLING\_NO"\].ToString().Trim()) > )
{
if (dr\[j\]\["STATION\_TYPE\_CODE"\].ToString().Trim() == "" || dr\[j\]\["STATION\_TYPE\_CODE"\].ToString().Trim() == "")
{
table.Cell(m + , ).Range.Text = "□ " + dr\[j\]\["PRINT\_TAG\_NAME"\].ToString().Trim();
if (strModType != dr\[j\]\["MODTYPE"\].ToString().Trim())
{
strModType = dr\[j\]\["MODTYPE"\].ToString().Trim();
if (strCollingNo.IndexOf(" 排号 " + dr\[j\]\["CALLING\_NO"\].ToString()) < )
{
strCollingNo = strCollingNo + "\\r\\n" + dr\[j\]\["MODTYPE"\].ToString().Trim() + " 排号 " + dr\[j\]\["CALLING\_NO"\].ToString();
}
}
}
else
{
table.Cell(m + , ).Range.Text = "□ " + dr\[j\]\["PRINT\_TAG\_NAME"\].ToString().Trim() + " 排号 " + dr\[j\]\["CALLING\_NO"\].ToString();
}
table.Cell(m + , ).Range.Font.Name = "微软雅黑";
table.Cell(m + , ).Range.Font.Size = 10.5F;
}
else
{
table.Cell(m + , ).Range.Text = "□ " + dr\[j\]\["PRINT\_TAG\_NAME"\].ToString().Trim();
table.Cell(m + , ).Range.Font.Name = "微软雅黑";
table.Cell(m + , ).Range.Font.Size = 10.5F;
}
options.Width = ;
options.Height = ;
options.Margin = ;
options.PureBarcode = true;
Image BarCodeNew = bw.Write(dr\[j\]\["OBSERVATION\_NO"\].ToString());
Clipboard.SetDataObject(BarCodeNew);
table.Cell(m + , ).Range.Paste();
table.Cell(m + , ).Range.Text += dr\[j\]\["OBSERVATION\_NO"\].ToString();
//table.Rows.Add(ref Nothing);
//table.Cell(m + 2, 2).Range.Text = dr\[j\]\["OBSERVATION\_NO"\].ToString();
BarCodeNew.Dispose();
}
if (strMemo != "" && dr\[j\]\["SHEET\_REMARKS"\].ToString().Trim() != "" && strMemo.IndexOf(dr\[j\]\["SHEET\_REMARKS"\].ToString().Trim() + ",") < )
{
strMemo += dr\[j\]\["SHEET\_REMARKS"\].ToString().Trim() + ",";
}
}
else
{
continue;
}
#endregion
}
#endregion
}
WordDoc.Tables\[x\].Cell(iRow, ).Range.Text = dr\[\]\["STATION\_NAME"\].ToString().Trim() + strCollingNo;
}
if (dttemp.Rows\[i\]\["FLOW\_SHEET\_MEMO"\].ToString().Trim() != "" || strMemo != "")
{
WordDoc.Content.Tables\[x\].Rows.Add(ref Nothing);
iRow++;
WordDoc.Tables\[x\].Cell(iRow, ).Merge(WordDoc.Tables\[x\].Cell(iRow, ));
WordDoc.Tables\[x\].Cell(iRow, ).Width = + 70.5f;
WordDoc.Tables\[x\].Cell(iRow, ).Width = ;
WordDoc.Tables\[x\].Cell(iRow, ).Range.Text = (dttemp.Rows\[i\]\["FLOW\_SHEET\_MEMO"\].ToString().Length > ? dttemp.Rows\[i\]\["FLOW\_SHEET\_MEMO"\].ToString() + "," : "") + strMemo;
WordDoc.Tables\[x\].Cell(iRow, ).Select();
WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
blMerge = true;
}
}
WordDoc.Content.Tables\[x\].Rows.Add(ref Nothing);
WordDoc.Tables\[x\].Cell(iRow + , ).Range.Text = "(1)登记报到 指引单缴回";
if (dtMome.Rows.Count > )
{
WordDoc.Tables\[x\].Cell(, ).Range.Text = dtMome.Rows\[\]\["MEMO\_HEAD"\].ToString();
}
if (WordDoc.Bookmarks.Exists("MEMO\_TAIL"))
{
WordDoc.Bookmarks.get\_Item("MEMO\_TAIL").Range.Text = dtMome.Rows\[\]\["MEMO\_TAIL"\].ToString();
}
if (strPrintFlag != "Y")
{
SaveFileDialog sfd = new SaveFileDialog();
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
sfd.FileName = strFileName + ".doc";
object oMissing = System.Reflection.Missing.Value;
object ofilename = sfd.FileName;
WordDoc.SaveAs(ref ofilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
WordApp.Visible = false;
WordDoc.Close();
WordApp.Quit();
}
else
{
string defaultPrinter = WordApp.ActivePrinter;
WordApp.PrintPreview = false;
WordApp.ActivePrinter = strWordPrint;
WordApp.PrintOut();
object oMissing = System.Reflection.Missing.Value;
WordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
WordDoc.Saved = true;
WordApp.ActivePrinter = defaultPrinter;
WordApp.Quit();
}
BarCode.Dispose();
}
catch (Exception ex)
{
WordApp.Quit();
throw ex;
}
finally
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
{
p.Kill();
}
GC.Collect();
}
}
private Table CreatTable(int iColumn)
{
object Nothing = System.Reflection.Missing.Value;
Range rng = WordApp.Selection.Range;
rng.ParagraphFormat.LineSpacing = 15F;
Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(rng, , iColumn, ref Nothing, ref Nothing);
if (iColumn == )
{
table.Columns\[\].Width = ;
table.Columns\[\].Width = ;
}
table.Rows.Alignment = WdRowAlignment.wdAlignRowLeft;
table.Range.Font.Name = "微软雅黑";
table.Range.Font.Size = 10.5F;
return table;
}
手动穿件Word.dot 模板文档格式如下,插入相应标签
手机扫一扫
移动阅读更方便
你可能感兴趣的文章