通过excel可识别的xml结构直接生成xls文件2009-12-29 csdn 贾涛上一片文章演示了如何根据简单的excel文件结构直接生成xls文件,如果涉及到合并,公式之类的复 杂操作,可以使用xml结构来直接构造xls文件,比如生成如下所示文件

上图中D列和E列为 公式,第4行为公式合计,7、8行为合并过的单元格。完整代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Xml;
namespaceConsoleApplication17
{
classProgram
{
static voidMain( string [] args)
{
ExcelWriter excel =newExcelWriter();
excel.CreateSheet( "XmlData" ); //sheetName
//增加一列,默认可以不加
excel.CreateColumn(5, 100);
//新增表头行
excel.CreateRow();
excel.CreateCellString( "Name" );
excel.CreateCellString( "Score1" );
excel.CreateCellString( "Score1" );
excel.CreateCellString( "Score0" );
excel.CreateCellString( "说明" );
//新增两行数据
excel.CreateRow();
excel.CreateCellString( "jinjazz" );
excel.CreateCellNumber(100);
excel.CreateCellNumber(98);
excel.CreateCell(0,"Number" ,"RC[-2]+RC[-1]" ,1,1);//公式,-2和-1代表当前cell的水平偏移量
excel.CreateCell(0,"String" ,"RC[-4]&":"&RC[-1]" , 1, 1); //公式
excel.CreateRow();
excel.CreateCellString( "游客" );
excel.CreateCellNumber(33);
excel.CreateCellNumber(14);
excel.CreateCell(0,"Number" ,"RC[-2]+RC[-1]" , 1, 1);
excel.CreateCell(0,"String" ,"RC[-4]&":"&RC[-1]" , 1, 1);
//新增汇总行
excel.CreateRow();
excel.CreateCellString( "总计" );
excel.CreateCell(0,"Number" ,"SUM(R[-2]C:R[-1]C)" , 1, 1); //公式,-2和-1代表cell的垂直偏移量
excel.CreateCell(0,"Number" ,"SUM(R[-2]C:R[-1]C)" , 1, 1);
excel.CreateCell(0,"Number" ,"SUM(R[-2]C:R[-1]C)" , 1, 1);
//增加三个空行
excel.CreateRow();
excel.CreateRow();
excel.CreateRow();
//增加一个合并过的单元格
excel.CreateCell( "http://blog.csdn.net/jinjazz" , "String" , null ,2,5);
excel.Save(@ "c: estData.xls" );
}
}
public classExcelWriter
{
stringssns ="urn:schemas-microsoft-com:office:spreadsheet" ;
stringxmlns ="urn:schemas-microsoft-com:office:spreadsheet" ;
XmlDocument _doc =newXmlDocument();
XmlNode _currentSheet =null ;
XmlNode _currentRow =null ;
publicExcelWriter()
{
//excel的xml模版,你需要了解xml的Attributes怎么用
StringBuilder sbody =newStringBuilder();
sbody.Append( "<?xml version="1.0"?>
" );
sbody.Append( "<?mso-application progid="Excel.Sheet"?>
" );
sbody.Append( "<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
" );
sbody.Append( "xmlns:o="urn:schemas-microsoft-com:office:office"
" );
sbody.Append( "xmlns:x="urn:schemas-microsoft-com:office:excel"
" );
sbody.Append( "xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
" );
sbody.Append( "xmlns:html="http://www.w3.org/TR/REC-html40">
" );
sbody.Append( "<Styles>
" );
sbody.Append( "<Style ss:ID="Default" ss:Name="Normal">
" );
sbody.Append( "<Alignment ss:Vertical="Center"/>
" );
sbody.Append( "<Borders/>
" );
sbody.Append( "<Font ss:FontName="宋体" x:CharSet="134" ss:Size="10"/>
" );
sbody.Append( "<Interior/>
" );
sbody.Append( "<NumberFormat/>
" );
sbody.Append( "<Protection/>
" );
sbody.Append( "</Style>
" );
sbody.Append( "</Styles>
" );
sbody.Append( "</Workbook>
" );
_doc.LoadXml(sbody.ToString());
}
/// <summary>
/// 增加一个工作表
/// </summary>
/// <param name="sheetName">工作表名称</param>
public voidCreateSheet( stringsheetName)
{
System.Xml.XmlNode node = _doc.CreateNode(XmlNodeType.Element,"Worksheet" , ssns);
System.Xml.XmlAttribute xa = _doc.CreateAttribute( "ss" ,"Name" , xmlns);
xa.Value = sheetName;
node.Attributes.Append(xa);
_doc.ChildNodes[2].AppendChild(node);
node.AppendChild(_doc.CreateNode(XmlNodeType.Element,"Table" , xmlns));
_currentSheet = node;
}
/// <summary>
/// 增加一行
/// </summary>
public voidCreateRow()
{
System.Xml.XmlNode node = _doc.CreateNode(XmlNodeType.Element,"Row" , xmlns);
_currentSheet.ChildNodes[0].AppendChild(node);
_currentRow = node;
}
/// <summary>
/// 增加一列
/// </summary>
/// <param name="index">索引</param>
/// <param name="width">宽度</param>
public voidCreateColumn( intindex, floatwidth)
{
System.Xml.XmlNode node = _doc.CreateNode(XmlNodeType.Element,"Column" , xmlns);
System.Xml.XmlAttribute xa = _doc.CreateAttribute( "ss" ,"Index" , xmlns);
xa.Value = index.ToString();
node.Attributes.Append(xa);
xa = _doc.CreateAttribute( "ss" ,"Width" , xmlns);
xa.Value = width.ToString();
node.Attributes.Append(xa);
_currentSheet.ChildNodes[0].AppendChild(node);
}
/// <summary>
/// 增加一个单元格
/// </summary>
/// <param name="value">值</param>
/// <param name="Type">类型</param>
/// <param name="Expression">公式</param>
/// <param name="rowSpan">跨行</param>
/// <param name="colSpan">跨列</param>
public voidCreateCell( objectvalue,stringType,stringExpression,introwSpan,intcolSpan)
{
System.Xml.XmlAttribute xa =null ;
System.Xml.XmlNode nodeCell = _doc.CreateNode(XmlNodeType.Element,"Cell" , xmlns);
_currentRow.AppendChild(nodeCell);
if(! string .IsNullOrEmpty(Expression))
{
xa = _doc.CreateAttribute( "ss" ,"Formula" , xmlns);
xa.Value ="="+ Expression;
nodeCell.Attributes.Append(xa);
}
if(--colSpan > 0)
{
xa = _doc.CreateAttribute( "ss" ,"MergeAcross" , xmlns);
xa.Value = colSpan.ToString();
nodeCell.Attributes.Append(xa);
}
if(--rowSpan > 0)
{
xa = _doc.CreateAttribute( "ss" ,"MergeDown" , xmlns);
xa.Value = rowSpan.ToString();
nodeCell.Attributes.Append(xa);
}
System.Xml.XmlNode nodeData = _doc.CreateNode(XmlNodeType.Element,"Data" , xmlns);
xa = _doc.CreateAttribute( "ss" ,"Type" , xmlns);
xa.Value = Type;
nodeData.Attributes.Append(xa);
nodeData.InnerText = value.ToString();
nodeCell.AppendChild(nodeData);
}
/// <summary>
/// 增加一个数字单元格
/// </summary>
/// <param name="value"></param>
public voidCreateCellNumber( doublevalue)
{
CreateCell(value,"Number" ,null , 1, 1);
}
/// <summary>
/// 增加一个字符串单元格
/// </summary>
/// <param name="value"></param>
public voidCreateCellString( stringvalue)
{
CreateCell(value,"String" ,null , 1, 1);
}
/// <summary>
/// 保存
/// </summary>
/// <param name="strFile"></param>
public voidSave( stringstrFile)
{
_doc.Save(strFile);
}
}
}
上面代码基本都是对xml文件的操作,需要你对xml的dom对象比较熟悉,尤其 是Attributes的使用。