Word报表生成
/// <summary>
/// 生产报表
/// </summary>
/// <param name="strTemplate"></param>
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;
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 模板文档格式如下,插入相应标签
Word报表生成的更多相关文章
- C# 生成word 文档 代码 外加 IIS报错解决方案
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- iReport报表生成html,pdf,xls,word工具类
package com.report; import java.io.ByteArrayOutputStream;import java.io.File;import java.io.InputStr ...
- C#服务器端生成报告文档:使用帆软报表生成Word、Pdf报告
一.帆软报表简介 报表工具中,帆软报表相比Crystal Report(水晶报表).SQL Server Report Service(SSRS)等报表工具来说算是佼佼者,此外帆软报表在统计图表.数据 ...
- Delphi中编辑word
其他(28) //启动Word try wordapplication1.connect; except messagedlg('word may not be ins ...
- Delphi读取Word
Delphi读取Word现在关于往Word中写入数据的方法比较多,现在专门开个贴子,希望大家把自己读取Word内容的心得体会说一下,包括读取word文档中,有几个段落,如何读取第几个段落,读取有拼音的 ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- spring boot freemarker 导出word 带echarts图形报表
创建word文件内容如下 将word导出为xml格式 将文件后缀名改为 .ftl 在springboot项目中添加freemarker依赖 <!-- 导出word文档--> <dep ...
- Word/Excel 在线预览
前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...
- C#中5步完成word文档打印的方法
在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...
随机推荐
- ODBC连接数据库实例
2012-12-13 22:27 (分类:默认分类) 1.首先建立数据源,正常情况下载控制面板-管理工具-数据源,打开后有用户DSN系统DSN 两者区别在于系统级的DSN,就是对该系统的所有登录用户可 ...
- ORB-SLAM2 论文&代码学习 —— LocalMapping 线程
转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12360913.html 本文要点: ORB-SLAM2 Local ...
- K8S ? K3S !
K8S ? K3S ! K3S 踩坑开始 歪比歪比(奇怪的服务器) 服务器选择我熟悉的 Centos K3S内置 Containerd 但是!作为一个服务器使用自然是要用常见的一点的容器 Docker ...
- Web登录配置-H3C-S5120-LI系列
1.开启http服务和修改端口号 ip http enable ip http port 80 2.添加用户和密码.用户名:admin.密码:admin@123 [Sysname] local-us ...
- C# DateTime 工具类
项目gitHub地址 点我跳转 今天给大家带来一个C#里面的时间工具类,具体的直接看下面代码 using System; namespace ToolBox.DateTimeTool { public ...
- mysql数据库技术1——基本的增删查改的sql语句
1.数据库语言的分类 DDL:数据库定义语言 data Definition language 用于创建.修改.和删除数据库内的数据结构,如: 1:创建和删除数据库(CREATE DATABASE | ...
- (vue操作storage)Vue plugin for work with local storage,session storage and memo
vue-ls https://www.npmjs.com/package/vue-ls NPM npm install vue-ls --save Yarn yarn add vue-ls Usage ...
- AD常用命令以及概念
活动目录服务器常用命令合集如下: net accounts 查看第一台域控的计算机角色net accounts 查看计算机角色net share 查看共享netdom query fs ...
- 折腾vue--环境搭建(一)
1.安装nodejs nodejs下载地址:https://nodejs.org/en/ 2.检测nodejs //检测nodejs版本 node -v //检测npm npm –v 3.安装vue ...
- 纪中17日T2 2322. capacitor
2322. capacitor (File IO): input:capacitor.in output:capacitor.out 题目描述 输入 输出 样例输入 样例输出 数据范围限制 Solut ...