ASP.NET Simple OOXML匯出Excel,Word

來源:互聯網
上載者:User

1)簡介

  Simple OOXML(http://simpleooxml.codeplex.com/)是一個基於Open Office SDK v 2.0,同時支援office2007 Excel和Word匯出的開源類庫。支援.net 3.5及以上版本。對於Excel支援多種資料格式包括字串、數字、日期等,支援刪除活頁簿,支援DataTable的粘貼,支援合併儲存格和設定儲存格樣式等等。對於Word支援通過書籤定位來輸出欄位的功能。

  :本地下載

2)匯出Excel

        protected void Button1_Click(object sender, EventArgs e)        {            MemoryStream stream = SpreadsheetReader.Create();            SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true);            WorksheetPart worksheetPart = SpreadsheetReader.GetWorksheetPartByName(doc, "Sheet1");            WorksheetWriter writer = new WorksheetWriter(doc, worksheetPart);            writer.PasteText("B2", "Hello World");                        //Save to the memory stream            SpreadsheetWriter.Save(doc);                        //Write to response stream            Response.Clear();            Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", "example1.xlsx"));            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";            stream.WriteTo(Response.OutputStream);            Response.End();        }

  如果是現有Excel模版的匯出則可以把

MemoryStream stream = SpreadsheetReader.Create();

  修改為

MemoryStream stream = SpreadsheetReader.Copy(“columnstemplate.xlsx");

3)匯出Word

        public void DocumentPasteTest()        {            MemoryStream stream = DocumentReader.Copy(string.Format("{0}\\template.docx", TestContext.TestDeploymentDir));            WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);                        MainDocumentPart mainPart = doc.MainDocumentPart;            DocumentWriter writer = new DocumentWriter(mainPart);                        Text name = writer.PasteText("Koos van der Merwe", "NAME");            Text age = writer.PasteText("53", "AGE");            //Save to the memory stream, and then to a file            writer.Save();            DocumentWriter.StreamToFile(string.Format("{0}\\templatetest.docx", GetOutputFolder()), stream);        }

  通過Word的書籤功能實現粘貼文本的定位。

 

 

4)結語

  Simple OOXML是一個功能非常全面好用的匯出Excel和Word的解決方案,唯一美工不足是不支援office 2003的匯出,希望對於.NET匯出資料的朋友有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.