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打印,这的确会很棘手.一般如果要想选 ...
随机推荐
- Mysql 字符问题
先看一下mysql支持的字符范围 *数值类型:1.整形: 类型 大小 范围 ...
- 杭电-------2052Picture(C语言)
#include<stdio.h> int main() { int width, height; int i, j; while (~scanf("%d %d", & ...
- 解决github图片不显示的问题
修改hosts C:\Windows\System32\drivers\etc\hosts 在文件末尾添加: # GitHub Start 192.30.253.112 Build software ...
- vue中阻止事件穿透的方法
默认情况下,事件在h5页面会穿透传递,比如一div里面套一个div,点击上层div,下层div也会响应 要阻止事件穿透,使用event.stopPropagation(); 代码示例: <div ...
- 配置 Apache James 邮件服务器以使用加密邮件通讯协议
可先参照: 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8)https://www.cnblogs.com ...
- 剑指offer-面试题63-股票的最大利润-数组
/* 题目: 给定一个股价序列,求一次交易的最大利润. */ #include<iostream> #include<vector> using namespace std; ...
- 你为什么不来了解一下Python?
一.什么是Python Python [1](英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum发明. ...
- Python基础知识总结笔记(四)函数
Python基础知识总结笔记(四)函数python中的函数函数中的参数变量作用域偏函数PFA递归函数高阶函数BIFs中的高阶函数匿名函数lambda闭包Closure装饰器Decorator函数式编程 ...
- VSTO开发指南(VB2013版) 第一章 Office对象模型
完美地将visual basic和office 办公软件结合起来.来自微软公司VSTO小组的权威专家所编著. 全书共712页,内容极其全面而深入,猛一看,厚地犹如庞然大物.看完离大神就不远了哦< ...
- SpringBoot整合NoSql--(四)Session共享
简介: 正常情况下,HttpSession是通过Servlet 容器创建并进行管理的,创建成功之后都是保存在内存中.如果开发者需要对项目进行横向扩展搭建集群,那么可以利用一些硬件或者软件工具来做负载均 ...