C#Execl
2018-06-23 23:52:13来源:未知 阅读 ()
using System.IO; using System.Text; namespace iLIS.Common { /// <summary> /// 生成Excel文档内容 /// 存入工作流 /// </summary> public class ExcelDocumentx { private readonly StreamWriter _streamWriter; public ExcelDocumentx(Stream stream) { _streamWriter = new StreamWriter(stream, Encoding.UTF8); } /// <summary> /// 写入Excel文件头 /// </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: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:office'> <Author>Hitek</Author> <Company>HitekSoft(C) Ltd.,</Company> <Version>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='134' 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:Weight='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> /// 添加工作表 /// </summary> /// <param name="name">表单名称</param> /// <param name="defaultRowHeight">默认行高</param> /// <param name="defaultColumnWidth">默认列宽</param> public void BeginSheet(string name, double defaultRowHeight = 0, double defaultColumnWidth = 0) { _streamWriter.WriteLine("<Worksheet ss:Name='" + name + "'>"); _streamWriter.Write("<Table"); //默认行高 if (defaultRowHeight > 0.0001) _streamWriter.Write(string.Format(" ss:DefaultRowHeight='{0}'", defaultRowHeight)); //默认列宽 if (defaultColumnWidth > 0.0001) _streamWriter.Write(string.Format(" ss:ss:DefaultColumnWidth='{0}'", defaultColumnWidth)); _streamWriter.WriteLine(">"); } /// <summary> /// 添加标题行 /// </summary> /// <param name="colNames">标题行的名称</param> /// <param name="colWidths">标题行的列宽</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> /// 添加一行 /// </summary> /// <param name="styleName">样式名称</param> /// <param name="vals"></param> public void AddRow(object[] vals, string styleName = null) { if (string.IsNullOrEmpty(styleName)) styleName = "sBD"; _streamWriter.WriteLine("<Row>"); foreach (var val in vals) { string strval = val == null ? "" : val.ToString() .Replace("<", "<") .Replace(">", ">"); _streamWriter.WriteLine("<Cell ss:StyleID='{0}'><Data ss:Type='String'>{1}</Data></Cell>\n", styleName, strval); } _streamWriter.WriteLine("</Row>"); } /// <summary> /// 完成表单 /// </summary> public void EndSheet() { _streamWriter.WriteLine("</Table>"); _streamWriter.WriteLine("</Worksheet>"); } /// <summary> /// 写入Excel文件结束 完成导出 /// </summary> public void End() { _streamWriter.WriteLine("</Workbook>"); _streamWriter.Close(); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- How to Create Responsive Tables using CSS without &l 2020-04-30
- FCC---CSS Flexbox: Align Elements Using the justify-cont 2019-12-08
- 倒计时5,4,3,2,1css实现(count down from 5 to 1 using 2018-10-29
- .net 公共基础类 2018-06-24
- 事件管理 2018-06-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash