Net 自訂Excel模板匯出資料

來源:互聯網
上載者:User

標籤:

轉載自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral


 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Aspose.Cells; 8 using System.Text; 9 using System.Data;10 11 namespace Oceansoft.Net.CeSWeb.Module.匯出Excel_通過模板_12 {13     public partial class daochu : System.Web.UI.Page14     {15         protected void Page_Load(object sender, EventArgs e)16         {17 18         }19         /// <summary>20         /// 單擊匯出21         /// </summary>22         /// <param name="sender"></param>23         /// <param name="e"></param>24         public void btnDaochu_ServerClick(object sender, EventArgs e)25         {26             string file = Server.MapPath("~/Excel/ZWxxtj.xls");27             DataSet ds = new DataSet();28             DataTable dtout = new DataTable();29             dtout.Columns.Add("單位", typeof(string));30             dtout.Columns.Add("數量", typeof(string));31             dtout.Columns.Add("分數", typeof(string));32             dtout.Columns.Add("看看", typeof(string));33 34             List<DataTable> lst = null;35             for (int i = 0; i < 3; i++)36             {37                 lst = new List<DataTable>();38 39                 DataRow dr = dtout.NewRow();40                 dr["單位"] = "AAAA" + i;41                 dr["數量"] = "BBBB" + i;42                 dr["分數"] = "CCCC" + i;43                 dr["看看"] = "DDDD" + i;44                 dtout.Rows.Add(dr);45 46             }47             ds.Tables.Add(dtout);48 49             lst.Add(ds.Tables[0]);50 51 52             ExportExcelModel(this.Response, lst, file, 4);//從第四行開始填充資料53         }54 55         public void ExportExcelModel(HttpResponse res, List<System.Data.DataTable> Datas, string ExcelTemplatePath, int FirstRow)56         {57             //Excel的路徑 是放excel模板的路徑58             WorkbookDesigner designer = new WorkbookDesigner();59             designer.Open(ExcelTemplatePath);60 61             Worksheet sheet = designer.Workbook.Worksheets[0];62             sheet.Cells.ImportDataTable(Datas[0], false, FirstRow, 0, true);63 64             var c11 = sheet.Cells[0, 0];//第一行 第一列65 66             c11.PutValue("我是標題,大家新年快樂。。。");67 68             SaveOptions s = new XlsSaveOptions(SaveFormat.Excel97To2003);69             string str = "";70 71             str = HttpUtility.UrlEncode("測試.xls", Encoding.UTF8).ToString();72 73 74             designer.Workbook.Save(res, str, ContentDisposition.Attachment, s);75 76         }77     }78 }

Net 自訂Excel模板匯出資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.