按模板生成word报表,可扩展,demo可生成多页
2012-02-28 17:34
232 查看
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Data.SqlClient;
using System.Web;
using System.Data;
namespace MyPublicClass
{
/// <summary>
/// 报表名称,和模板名称一致, 该枚举可无限扩展
/// </summary>
public enum WordTemplateType
{
管道吹扫清洗检验记录,
}
/// <summary>
/// 生成word报表
/// yiyanxiyin@qq.com
/// 2012-2-28
/// </summary>
public class WordReport
{
/// <summary>
/// 生成word报表文件
/// </summary>
/// <param name="wordTemplate">word模板</param>
/// <param name="parameter">参数, 自定任意参数</param>
/// <returns>生成的文件名</returns>
public static string ExportDataToWord(WordTemplateType wordTemplate, object[] parameter)
{
Microsoft.Office.Interop.Word.Document wDoc = null;
Microsoft.Office.Interop.Word.Application wApp = null;
string filePath = "", appPath;
filePath = System.Web.HttpContext.Current.Server.MapPath("~/ExcelTemplate/" + wordTemplate.ToString() + ".doc");
OpenWordDoc(filePath, ref wDoc,ref wApp);
try
{
//扩展该switch定制报表
switch (wordTemplate)
{
case WordTemplateType.管道吹扫清洗检验记录:
Export_ClearAndCheckRecord(wApp,wDoc, parameter);
break;
}
filePath = filePath.Substring(0, filePath.Length - 4);
filePath = filePath.Replace("ExcelTemplate", "ExcelFiles");
filePath = getFileName(filePath, out appPath);
wDoc.SaveAs(filePath);
}
finally
{
if (wDoc != null)
{
wDoc.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}
if (wApp != null)
{
wApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}
GC.Collect();
}
return appPath;
}
private static void OpenWordDoc(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
if (FileName == "") return;
Microsoft.Office.Interop.Word.Document thisDocument = null;
Microsoft.Office.Interop.Word.FormFields formFields = null;
Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
Microsoft.Office.Interop.Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
}
private static string getFileName(string fileName, out string appFilePath)
{
System.DateTime currentTime = System.DateTime.Now;
Random rd = new Random();
string TempFileName = fileName + currentTime.ToString("yyyyMMddHHmm") + rd.Next().ToString() + ".doc";
string CurrentPath = System.Web.HttpContext.Current.Server.MapPath("~/ExcelFiles/");
string CurrentMonth = currentTime.ToString("yyyyMM");
if (!System.IO.Directory.Exists(CurrentPath + "\\" + CurrentMonth))
{
System.IO.Directory.CreateDirectory(CurrentPath + "\\" + CurrentMonth);
}
System.IO.FileInfo fi = new System.IO.FileInfo(TempFileName);
appFilePath = (HttpContext.Current.Request.ApplicationPath.EndsWith("/") ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "ExcelFiles/" + CurrentMonth + "/" + fi.Name;
appFilePath.Replace("//", "/");
return CurrentPath + CurrentMonth + "\\" + fi.Name;
}
/// <summary>
/// 管道吹扫清洗检验记录(可生成多页,每页12行)
/// </summary>
/// <param name="wSheet"></param>
/// <param name="wSheet"></param>
/// <param name="parameter">参数, 自定任意参数,在此函数中解析</param>
private static void Export_ClearAndCheckRecord(Application wApp, Document wDoc, object[] parameter)
{
string projectName = parameter[0].ToString();
string cellProject = parameter[1].ToString();
string packetNumber = parameter[2].ToString();
System.Data.DataTable detailDataTable = (System.Data.DataTable)parameter[3];
wDoc.Bookmarks["ProjectName"].Range.Text = projectName;
wDoc.Bookmarks["CellProject"].Range.Text = cellProject;
wDoc.Bookmarks["PacketNumber"].Range.Text = packetNumber;
if (detailDataTable.Rows.Count == 0) return;
//计算需要几个表格(每个表格显示12条数据)
int tablecount = detailDataTable.Rows.Count / 12 + (detailDataTable.Rows.Count % 12 == 0 ? 0 : 1);
//生成足够多的空表格
while (tablecount > 1)
{
wDoc.Tables[1].Select();
wApp.Selection.Copy();
object mymissing = System.Reflection.Missing.Value;
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
wApp.Selection.EndKey(ref myunit, ref mymissing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
wApp.Selection.InsertBreak(ref pBreak);
wApp.Selection.Paste();
tablecount--;
}
int i = 0;
foreach (DataRow dr in detailDataTable.Rows)
{
wDoc.Tables[i / 12 + 1].Cell(i % 12 + 9, 1).Range.Text = dr["pipelinecode"].ToString();
wDoc.Tables[i / 12 + 1].Cell(i % 12 + 9, 2).Range.Text = dr["material"].ToString();
i++;
}
}
//在此参考以上方法扩展自定报表
}
}实例word模板:
调用代码:
System.Data.DataSet ds = SqlData.Table_WeldTestPressure.ClearAndCheckRecordReport(projectId, singleProject, unitProject,cellProject, packetNumber);
string wordfile= WordReport.ExportDataToWord(WordTemplateType. 管道吹扫清洗检验记 录, new object[] { SqlData.Table_ProjectTable.GetProjectName(projectId), cellProject, packetNumber, ds.Tables[0] });
生成结果:
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Data.SqlClient;
using System.Web;
using System.Data;
namespace MyPublicClass
{
/// <summary>
/// 报表名称,和模板名称一致, 该枚举可无限扩展
/// </summary>
public enum WordTemplateType
{
管道吹扫清洗检验记录,
}
/// <summary>
/// 生成word报表
/// yiyanxiyin@qq.com
/// 2012-2-28
/// </summary>
public class WordReport
{
/// <summary>
/// 生成word报表文件
/// </summary>
/// <param name="wordTemplate">word模板</param>
/// <param name="parameter">参数, 自定任意参数</param>
/// <returns>生成的文件名</returns>
public static string ExportDataToWord(WordTemplateType wordTemplate, object[] parameter)
{
Microsoft.Office.Interop.Word.Document wDoc = null;
Microsoft.Office.Interop.Word.Application wApp = null;
string filePath = "", appPath;
filePath = System.Web.HttpContext.Current.Server.MapPath("~/ExcelTemplate/" + wordTemplate.ToString() + ".doc");
OpenWordDoc(filePath, ref wDoc,ref wApp);
try
{
//扩展该switch定制报表
switch (wordTemplate)
{
case WordTemplateType.管道吹扫清洗检验记录:
Export_ClearAndCheckRecord(wApp,wDoc, parameter);
break;
}
filePath = filePath.Substring(0, filePath.Length - 4);
filePath = filePath.Replace("ExcelTemplate", "ExcelFiles");
filePath = getFileName(filePath, out appPath);
wDoc.SaveAs(filePath);
}
finally
{
if (wDoc != null)
{
wDoc.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}
if (wApp != null)
{
wApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}
GC.Collect();
}
return appPath;
}
private static void OpenWordDoc(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
if (FileName == "") return;
Microsoft.Office.Interop.Word.Document thisDocument = null;
Microsoft.Office.Interop.Word.FormFields formFields = null;
Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
Microsoft.Office.Interop.Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
}
private static string getFileName(string fileName, out string appFilePath)
{
System.DateTime currentTime = System.DateTime.Now;
Random rd = new Random();
string TempFileName = fileName + currentTime.ToString("yyyyMMddHHmm") + rd.Next().ToString() + ".doc";
string CurrentPath = System.Web.HttpContext.Current.Server.MapPath("~/ExcelFiles/");
string CurrentMonth = currentTime.ToString("yyyyMM");
if (!System.IO.Directory.Exists(CurrentPath + "\\" + CurrentMonth))
{
System.IO.Directory.CreateDirectory(CurrentPath + "\\" + CurrentMonth);
}
System.IO.FileInfo fi = new System.IO.FileInfo(TempFileName);
appFilePath = (HttpContext.Current.Request.ApplicationPath.EndsWith("/") ? HttpContext.Current.Request.ApplicationPath : HttpContext.Current.Request.ApplicationPath + "/") + "ExcelFiles/" + CurrentMonth + "/" + fi.Name;
appFilePath.Replace("//", "/");
return CurrentPath + CurrentMonth + "\\" + fi.Name;
}
/// <summary>
/// 管道吹扫清洗检验记录(可生成多页,每页12行)
/// </summary>
/// <param name="wSheet"></param>
/// <param name="wSheet"></param>
/// <param name="parameter">参数, 自定任意参数,在此函数中解析</param>
private static void Export_ClearAndCheckRecord(Application wApp, Document wDoc, object[] parameter)
{
string projectName = parameter[0].ToString();
string cellProject = parameter[1].ToString();
string packetNumber = parameter[2].ToString();
System.Data.DataTable detailDataTable = (System.Data.DataTable)parameter[3];
wDoc.Bookmarks["ProjectName"].Range.Text = projectName;
wDoc.Bookmarks["CellProject"].Range.Text = cellProject;
wDoc.Bookmarks["PacketNumber"].Range.Text = packetNumber;
if (detailDataTable.Rows.Count == 0) return;
//计算需要几个表格(每个表格显示12条数据)
int tablecount = detailDataTable.Rows.Count / 12 + (detailDataTable.Rows.Count % 12 == 0 ? 0 : 1);
//生成足够多的空表格
while (tablecount > 1)
{
wDoc.Tables[1].Select();
wApp.Selection.Copy();
object mymissing = System.Reflection.Missing.Value;
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
wApp.Selection.EndKey(ref myunit, ref mymissing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
wApp.Selection.InsertBreak(ref pBreak);
wApp.Selection.Paste();
tablecount--;
}
int i = 0;
foreach (DataRow dr in detailDataTable.Rows)
{
wDoc.Tables[i / 12 + 1].Cell(i % 12 + 9, 1).Range.Text = dr["pipelinecode"].ToString();
wDoc.Tables[i / 12 + 1].Cell(i % 12 + 9, 2).Range.Text = dr["material"].ToString();
i++;
}
}
//在此参考以上方法扩展自定报表
}
}实例word模板:
SH/T3503-J408 | 管道吹扫/清洗检验记录 | 工程名称: 单元名称: | ||
系统名称 | 系统编号 | |||
检 查 项 目 与 要 求 | 检 查 结 果 | |||
管道耐压试验合格 | 合格 | |||
不参与吹扫/清洗的安全附件及仪表等已隔离或拆除 | 合格 | |||
管道系统的阀门已全部开启 | 合格 | |||
不锈钢管道用水符合规范要求 | 合格 | |||
管道编号 | 材质 | 操作介质 | 吹扫介质 | 清洗介质 |
检验方法及结论: | ||||
建设/监理单位 | 总 承 包 单 位 | 施 工 单 位 | ||
专业工程师: 日期: 年 月 日 | 专业工程师: 日期: 年 月 日 | 专业工程师: 质量检查员: 记 录 人: 日期: 年 月 日 |
System.Data.DataSet ds = SqlData.Table_WeldTestPressure.ClearAndCheckRecordReport(projectId, singleProject, unitProject,cellProject, packetNumber);
string wordfile= WordReport.ExportDataToWord(WordTemplateType. 管道吹扫清洗检验记 录, new object[] { SqlData.Table_ProjectTable.GetProjectName(projectId), cellProject, packetNumber, ds.Tables[0] });
生成结果:
SH/T3503-J408 | 管道吹扫/清洗检验记录 | 工程名称:焊接数据库测试 单元名称:焊接数据库测试 | |||||||
系统名称 | 系统编号 | 3 | |||||||
检 查 项 目 与 要 求 | 检 查 结 果 | ||||||||
管道耐压试验合格 | 合格 | ||||||||
不参与吹扫/清洗的安全附件及仪表等已隔离或拆除 | 合格 | ||||||||
管道系统的阀门已全部开启 | 合格 | ||||||||
不锈钢管道用水符合规范要求 | 合格 | ||||||||
管道编号 | 材质 | 操作介质 | 吹扫介质 | 清洗介质 | |||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 11 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 11 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWR-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 11 | ||||||||
检验方法及结论: | |||||||||
建设/监理单位 | 总 承 包 单 位 | 施 工 单 位 | |||||||
专业工程师: 日期: 年 月 日 | 专业工程师: 日期: 年 月 日 | 专业工程师: 质量检查员: 记 录 人: 日期: 年 月 日 | |||||||
SH/T3503-J408 | 管道吹扫/清洗检验记录 | 工程名称:焊接数据库测试 单元名称:焊接数据库测试 | |||||||
系统名称 | 系统编号 | 3 | |||||||
检 查 项 目 与 要 求 | 检 查 结 果 | ||||||||
管道耐压试验合格 | 合格 | ||||||||
不参与吹扫/清洗的安全附件及仪表等已隔离或拆除 | 合格 | ||||||||
管道系统的阀门已全部开启 | 合格 | ||||||||
不锈钢管道用水符合规范要求 | 合格 | ||||||||
管道编号 | 材质 | 操作介质 | 吹扫介质 | 清洗介质 | |||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 1 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 11 | ||||||||
检验方法及结论: | |||||||||
建设/监理单位 | 总 承 包 单 位 | 施 工 单 位 | |||||||
专业工程师: 日期: 年 月 日 | 专业工程师: 日期: 年 月 日 | 专业工程师: 质量检查员: 记 录 人: 日期: 年 月 日 | |||||||
SH/T3503-J408 | 管道吹扫/清洗检验记录 | 工程名称:焊接数据库测试 单元名称:焊接数据库测试 | |||||||
系统名称 | 系统编号 | 3 | |||||||
检 查 项 目 与 要 求 | 检 查 结 果 | ||||||||
管道耐压试验合格 | 合格 | ||||||||
不参与吹扫/清洗的安全附件及仪表等已隔离或拆除 | 合格 | ||||||||
管道系统的阀门已全部开启 | 合格 | ||||||||
不锈钢管道用水符合规范要求 | 合格 | ||||||||
管道编号 | 材质 | 操作介质 | 吹扫介质 | 清洗介质 | |||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 1 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
CWS-000301-1200-B1F01- | 22 | ||||||||
检验方法及结论: | |||||||||
建设/监理单位 | 总 承 包 单 位 | 施 工 单 位 | |||||||
专业工程师: 日期: 年 月 日 | 专业工程师: 日期: 年 月 日 | 专业工程师: 质量检查员: 记 录 人: 日期: 年 月 日 | |||||||
相关文章推荐
- 记录使用freemarker生成word的模板ftl文件以及word的遍历数据
- ASP.NET MVC 4.0 中使用NPOI 2.2.0 按模板生成Excel报表
- Java POI通过模板方法生成word文档
- [转]基于模板和XML在BS结构应用中生成word文件
- Java用模板生成word
- 根据word模板生成word表格报表文档(C#)
- 使用poi操作模板文件重新生成新的word表单文件
- Java Web项目中利用FreeMarker模板生成的word文档,其它格式文档同理,如pdf,txt,java,xml等等
- freemarker根据模板生成word步骤优化
- python工具之exccel模板生成报表
- Office编程在dot Net环境中总结(Word生成表格报表篇)
- 移动端无法打开FreeMarker模板生成的Word文档
- poi 读取Excel文件模板生成报表文件
- velocity模板技术生成word文档
- C# 根据Word 模板生成word 文件
- Office编程在dot Net环境中总结(Word生成表格报表篇) (一)
- VC中调用EXECL模板生成报表
- 利用freemarker 在模板里面写入动态数据,动态表格,图片插入并生成word文档
- 基于Java模板技术动态生成Word文档
- Java Web项目中利用FreeMarker模板生成的word文档,其它格式文档同理,如pdf,txt,java,xml等等