博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

按模板生成word报表,可扩展,demo可生成多页

Posted on 2012-02-28 17:34  yiyanxiyin  阅读(911)  评论(4编辑  收藏  举报
 
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 == 0return;

            //计算需要几个表格(每个表格显示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 + 91).Range.Text = dr["pipelinecode"].ToString();
                wDoc.Tables[i / 12 + 1].Cell(i % 12 + 92).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

 

 

 

 

 

 

 

 

检验方法及结论:

建设/监理单位

总 承 包 单 位

施 工 单 位

专业工程师:

 

 

 

日期:               

专业工程师:

 

 

 

日期:               

专业工程师:

质量检查员:

记 录 人:

 

日期:               

                   

 

 如不清楚的可以留言或者QQ:44048955