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打印,这的确会很棘手.一般如果要想选 ...
随机推荐
- php插件名称 yum安装
提示缺少 安装 DOM扩展模块 yum install php-xml PDO ...
- 修改定时清理 /tmp 目录下的文件
初衷 默认系统是超过 30 天不访问的文件自动清除的,但是有时候硬盘用得紧可以考虑修改周期 设置方法 编辑配置文件:vim /etc/cron.daily/tmpwatch #! /bin/sh fl ...
- 小记centos7.5下yum安装cobbler遇到的问题
问题1:执行cobbler sync同步命令报错,提示dhcpd服务错误和Python源码错误 [root@server ~]# cobbler sync #<===执行cobbler同步的时候 ...
- [Python]公司接口返回值规范
返回值规范 json { "code":200, "message":"" "data":[ { "title ...
- IT运维软件免费送 智和信通战疫活动火热进行中
突如其来的“新型冠状病毒”疫情牵动了全国人民的心,这是一场与病毒抗争,为生命逆行与时间赛跑的战役.举国上下众志成城面对疫情,在线医疗.在线教学.在线办公.在线会议.电商销售成为热点.线上经济的火热给企 ...
- String实例 (练习)
练习题1:用户输入一段字符串,要求统计出在该段字符串中,数字,字母以及其他字符各出现过几次??? 代码实现: 运行结果: 补充:1. 连接符的使用: +用作连接符时,只能连接字符串,即“ ”双 ...
- 手动发布本地jar包到Nexus私服
1.Nexus配置 1. 在Nexus私服上建立仓库,用于盛放jar包,如名叫3rd_part. 2. 注册用户Nuxus用户,如名叫dev,密码dev_123. 3. 给dev用户分配能访问3rd_ ...
- Ubuntu18.04安装mysql并配置远程访问
1.ssh连接到Ubuntu服务器 默认root用户登陆,如果运行以下命令没有权限请在命令开头加sudo 2.安装mysql apt install mysql-server 3.配置mysql my ...
- Orleans[NET Core 3.1] 学习笔记(四)( 3 )监控Orleans Silo的方式 OrleansDashboard
简介 Orleans用起来的确很爽,更爽的是咱们有能监控它的工具. OrleansDashboard 这个工具是一个可视化的Silo监控工具,Silo和Grain的活跃状态一目了然,各个接口的响应速度 ...
- P1196 [NOI2002]银河英雄传说 【带权并查集】
思路 用sum记录每个舰队的战舰数量, tohead 记录当前舰离舰首的距离,那么求任意两舰之间有多少舰显然就是 abs( tohead[i] - tohead[j] ) - 1: CODE #inc ...