博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Excel
阅读量:4974 次
发布时间:2019-06-12

本文共 3845 字,大约阅读时间需要 12 分钟。

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:officce: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'/>
<Inherior 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]));
}
}
AddHeaderRow(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();
}
}
}

转载于:https://www.cnblogs.com/zzp0320/p/7064699.html

你可能感兴趣的文章
大白话描述事件 [转]
查看>>
面试-java算法题
查看>>
IDEA类和方法注释模板设置(非常详细)
查看>>
eclipse生成并调用webservices客户端client
查看>>
Java--动态代理
查看>>
day05列表 类型
查看>>
闲聊测试工程师
查看>>
如何在线编辑修改PDF文件中的背景颜色
查看>>
angularJS 学习之路
查看>>
php header
查看>>
汇编第一章总结
查看>>
java-面向对象编程
查看>>
TCP协议
查看>>
【iOS学习笔记】01-开篇
查看>>
MySQL多实例安装
查看>>
用一个玩具例子说明基于视频的超分辨率重建的基本思想
查看>>
集训 D1T1 clique
查看>>
ContentProvider类的设计分析
查看>>
Codeforces 869E The Untended Antiquity
查看>>
boost 相关
查看>>