Use openXML to export excel and openxmlexcel without plug-ins

Source: Internet
Author: User

Use openXML to export excel and openxmlexcel without plug-ins

The comments are very detailed. If you have any questions, please ask.

Using System. IO; using System. text; namespace iLIS. common {// <summary> /// generate Excel document content /// save to workflow /// </summary> public class ExcelDocumentx {private readonly StreamWriter _ streamWriter; public ExcelDocumentx (Stream stream) {_ streamWriter = new StreamWriter (stream, Encoding. UTF8) ;}/// <summary> /// write the Excel file header /// </summary> public void Begin () {const string excelHeader = @ "<? Xml version = '1. 0'?> <? Mso-application progid = 'excel. Sheet '?> <Workbook xmlns = 'urn: schemas-microsoft-com: office: spreadsheet 'xmlns: o = 'urn: schemas-microsoft-com: office' xmlns: x = 'urn: schemas-microsoft-com: office: excel 'xmlns: ss = 'urn: schemas-microsoft-com: office: spreadsheet' xmlns: html = 'HTTP: // www.w3.org/TR/REC-html40'> <DocumentProperties xmlns = 'urn: schemas-microsoft-com: office '> <Author> Hitek </Author> <Company> HitekSoft (C) Ltd ., </Company> <Versi On> 12.00 </Version> </DocumentProperties> <Styles> <Style ss: ID = 'sh'> <Alignment ss: Vertical = 'center' ss: wrapText = '1'/> <Borders> <Border ss: Position = 'bottom' ss: LineStyle = 'continuous 'ss: Weight = '1'/> <Border ss: position = 'left' ss: LineStyle = 'continuous 'ss: Weight = '1'/> <Border ss: Position = 'right' ss: LineStyle = 'continuous' ss: weight = '1'/> <Border ss: Position = 'top' ss: LineStyle = 'continuous 'ss: Weight = '1'/> </Borders> <Font ss: FontName = ''x: CharSet = '000000' ss: Size = '11' ss: color = '#000000' ss: Bold = '1'/> <Interior ss: Color = '# F2F2F2' ss: pattern = 'solid'/> <NumberFormat/> <Protection/> </Style> <Style ss: ID = 'sbd'> <Alignment ss: Vertical = 'center' ss: wrapText = '1'/> <Borders> <Border ss: Position = 'bottom' ss: LineStyle = 'continuous 'ss: Weight = '1'/> <Border ss: position = 'left' ss: LineStyle = 'continuous 'ss: W Eight = '1'/> <Border ss: Position = 'right' ss: LineStyle = 'continuous 'ss: Weight = '1'/> <Border ss: position = 'top' ss: LineStyle = 'continuous 'ss: Weight = '1'/> </Borders> </Style> </Styles> "; _ streamWriter. writeLine (excelHeader );} /// <summary> /// Add a worksheet /// </summary> /// <param name = "name"> form name </param> /// <param name = "defaultRowHeight"> default row height </param> // <param name = "defaultColumnWidth"> default column width </param> publi C void BeginSheet (string name, double defaultRowHeight = 0, double defaultColumnWidth = 0) {_ streamWriter. writeLine ("<Worksheet ss: Name = '" + name + "'>"); _ streamWriter. write ("<Table"); // The default Row Height (if (defaultRowHeight> 0.0001) _ streamWriter. write (string. format ("ss: DefaultRowHeight = '{0}'", defaultRowHeight); // The default column width, if (defacolumcolumnwidth> 0.0001) _ streamWriter. write (string. format ("ss: DefaultCo LumnWidth = '{0}' ", defaultColumnWidth); _ streamWriter. writeLine ("> ");} /// <summary> /// Add the title line /// </summary> /// <param name = "colNames"> name of the title line </param> // /<param name = "colWidths"> column width of the title row </param> public void AddHeaderRow (string [] colNames, double [] colWidths = null) {// if (colWidths! = Null & colWidths. length> 0) {for (int I = 0; I <colWidths. length; I ++) {if (colWidths [I]> 0.0001) _ streamWriter. writeLine (string. format ("<Column ss: Index = '{0}' ss: AutoFitWidth = '0' ss: Width = '{1}'/>", I + 1, colWidths [I]) ;}} AddRow (colNames, "sH ");} /// <summary> /// Add a row /// </summary> /// <param name = "styleName"> style name </param> /// <param name = "vals"> </param> public void AddRow (object [] va Ls, string styleName = null) {if (string. isNullOrEmpty (styleName) styleName = "sBD"; _ streamWriter. writeLine ("<Row>"); foreach (var val in vals) {string strval = val = null? "": Val. toString (). replace ("<", "& lt ;"). replace (">", "& gt;"); _ streamWriter. writeLine ("<Cell ss: StyleID = '{0}'> <Data ss: Type = 'string'> {1} </Data> </Cell> \ n ", styleName, strval);} _ streamWriter. writeLine ("</Row>") ;}/// <summary >/// complete the form /// </summary> public void EndSheet () {_ streamWriter. writeLine ("</Table>"); _ streamWriter. writeLine ("</Worksheet>") ;}/// <summary> /// export after the Excel file is written. /// </summary> public void End () {_ streamWriter. writeLine ("</Workbook>"); _ streamWriter. close ();}}}
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.