apose 根據excel 匯出模版

(二少)在南極發表於2014-02-14

 string file = Server.MapPath("~/Excel/ZWxxtj.xls");
            DataSet ds = new DataSet();
            DataTable dtout = new DataTable();
            dtout.Columns.Add("單位", typeof(string));
            dtout.Columns.Add("數量", typeof(string));
            dtout.Columns.Add("分數", typeof(string));
            dtout.Columns.Add("看看", typeof(string));

            List<DataTable> lst = null;
            for (int i = 0; i < 3; i++)
            {
                lst = new List<DataTable>();

                DataRow dr = dtout.NewRow();
                dr["單位"] = "AAAA" + i;
                dr["數量"] = "BBBB" + i;
                dr["分數"] = "CCCC" + i;
                dr["看看"] = "DDDD" + i;
                dtout.Rows.Add(dr);

            }
            ds.Tables.Add(dtout);

            lst.Add(ds.Tables[0]);


            ExportExcelModel(this.Response, lst, file, 4);//從第四行開始填充資料

 

public void ExportExcelModel(HttpResponse res, List<System.Data.DataTable> Datas, string ExcelTemplatePath, int FirstRow)
        {
            //Excel的路徑 是放excel模板的路徑
            WorkbookDesigner designer = new WorkbookDesigner();
            designer.Open(ExcelTemplatePath);

            Worksheet sheet = designer.Workbook.Worksheets[0];
            sheet.Cells.ImportDataTable(Datas[0], false, FirstRow, 0, true);

            var c11 = sheet.Cells[0, 0];//第一行 第一列

            c11.PutValue("我是標題,大家新年快樂。。。");

            SaveOptions s = new XlsSaveOptions(SaveFormat.Excel97To2003);
            string str = "";

            str = HttpUtility.UrlEncode("測試.xls", Encoding.UTF8).ToString();


            designer.Workbook.Save(res, str, ContentDisposition.Attachment, s);

        }

 

相關文章