当前位置:编程学习 > C#/ASP.NET >>

C#导出excel,固定模板问题

现在要导出一个excel,但是模板是之前做好的,我想吧数据直接追加在excel后面,格式还是按照模板格式, --------------------编程问答-------------------- 没人嘛 --------------------编程问答-------------------- 自己顶 --------------------编程问答-------------------- 什么“模板格式”? --------------------编程问答-------------------- 希望对你有用

using System.Reflection;  //MISSIN
using Microsoft.Office.Interop.Excel;    //APP
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Data.SqlClient;


public void outputExcel(System.Data.DataTable dataTable, string templateName)
    {
        string url = "";
        //获取template文件夹路经,UserExcel.aspx和template文件夹在同一父目录下
        string Path = this.GetFileFolder("OutExcel.aspx") + "template//";
        string XmlPath = Path;

        //使用excel
        Application app = new Application();
        Process[] pro = Process.GetProcessesByName("EXCEL");
        int proID = pro[pro.Length - 1].Id;
        app.DisplayAlerts = false;
        try
        {
            //打开模板
            string fileName = XmlPath + templateName + ".xlt";
            object template_p = fileName;
            object newTemplate_p = false;
            object visible_p = true;
            object updateLink_p = Missing.Value;
            object ReadOnly_p = false;
            object Format_p = Missing.Value;
            object password = "";
            object Writepassword = "";
            object Recommended_p = Missing.Value;
            object Origin_p = 2;
            object Delimitit_p = Missing.Value;
            object Editable_p = true;
            object Notify_p = Missing.Value;
            object Converter_p = Missing.Value;
            object AddToMru_p = Missing.Value;
            object local_p = Missing.Value;
            object corruptload_p = Missing.Value;

            Microsoft.Office.Interop.Excel._Workbook wc = app.Workbooks._Open(fileName, updateLink_p, ReadOnly_p, Format_p, password, Writepassword, Recommended_p, Origin_p, Delimitit_p, Editable_p, Notify_p, Converter_p, AddToMru_p);
            //定位sheet
            _Worksheet oSheet = (_Worksheet)wc.Worksheets[1];

            switch (templateName)
            {
                case "TraficAcciStatMonthReport":
                    oSheet.Cells[2, 5] = dataTable.Rows[0]["ReportMonth"].ToString();// 报表月份
                    //oSheet.Cells[7, 2] = dataTable.Rows[0]["AccidentNum"].ToString();// 本月事故起数
                    //oSheet.Cells[8, 2] = dataTable.Rows[0]["AccidentSum"].ToString();// 本月事故起数
                    oSheet.Cells[7, 2] = dataTable.Rows[0]["DeathAcciNum"].ToString();// 本月事故起数
                    oSheet.Cells[8, 2] = dataTable.Rows[0]["DeathAcciSum"].ToString();

                    oSheet.Cells[7, 3] = dataTable.Rows[0]["PreDeathAcciNum"].ToString();
                    oSheet.Cells[8, 3] = dataTable.Rows[0]["PreDeathAcciSum"].ToString();                                        
                     
                    break;
                
                    

            }
            //templateName
            
            //保存文件
            System.Random rand = new System.Random();
            int a = rand.Next();
            string ss = a.ToString();
            string pathStr;
            pathStr = XmlPath.Remove(XmlPath.Length - 1, 1);
            int position = pathStr.LastIndexOf("\\");
            pathStr = pathStr.Remove(position + 1, pathStr.Length - position - 1);
            pathStr = pathStr + "trush\\";
            object fileName_p = pathStr + ss + ".xls"; ;
            object fileFormat_p = Missing.Value;
            object password_p = Missing.Value;
            object writePassword_p = Missing.Value;
            XlSaveAsAccessMode readOnlyRecommended_p = new XlSaveAsAccessMode();
            object CreateBackup_p = Missing.Value;
            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode accessmode_p = new XlSaveAsAccessMode();
            object conflictResolutuin = Missing.Value;
            object addtoMru_p = Missing.Value;
            object Textcode_p = Missing.Value;
            object textVisuall_p = Missing.Value;
            wc._SaveAs(fileName_p, fileFormat_p, password, writePassword_p, readOnlyRecommended_p, CreateBackup_p, accessmode_p, conflictResolutuin, addtoMru_p, Textcode_p, textVisuall_p);
            ///关闭模板
            object saveChanges_p = Missing.Value;
            object originalFormat_p = Missing.Value;
            object routeDocument_p = Missing.Value;
            wc.Close(saveChanges_p, originalFormat_p, routeDocument_p);

            app.Quit();
            app = null;
            wc = null;
            if (proID != 0)
                KillallTimer(proID);
            //url = this.Request.Url.ToString();// ;//.AbsoluteUri;

            url = this.Request.Url.AbsoluteUri;

            url = url.Substring(0, url.Length - "OutExcel.aspx".Length) + "trush\\";
            //url = url + "trush\\";
            url = url + ss + ".xls";
            Response.Write(@"<script language=javascript>window.close();window.open('" + url.Replace("\\", "/") + "')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            if (proID != 0)
                KillallTimer(proID);
        }
    }






private void KillallTimer(int ProcessID)
    {
        try
        {
            GC.Collect();
            System.Diagnostics.Process[] pw = System.Diagnostics.Process.GetProcessesByName("WINWORD");
            for (int i = 0; i < pw.Length; i++)
            {
                System.IntPtr c = pw[i].MainWindowHandle;
                if (c.ToInt32() == 0)
                {
                    if (pw[i].Id == ProcessID)
                    {
                        pw[i].Kill();
                    }
                }
            }
            GC.Collect();
        }
        catch
        {
            GC.Collect();
        }
    }
--------------------编程问答-------------------- “按照模板格式”,也就是说模板的格式改变了那么输出的结果也就改变。

一定不是写死什么 oSheet.Cells[2, 5] 之类的。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,