通过excel可识别的xml结构直接生成xls文件

时间:2022-06-26 12:42:35

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

通过excel可识别的xml结构直接生成xls文件

上图中D列和E列为公式,第4行为公式合计,7、8行为合并过的单元格。完整代码如下:

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Text;
  4. using  System.Xml;
  5. namespace  ConsoleApplication17
  6. {
  7.      class  Program
  8.     {
  9.          static   void  Main( string [] args)
  10.         {
  11.             
  12.             ExcelWriter excel =  new  ExcelWriter();
  13.             excel.CreateSheet( "XmlData" ); //sheetName
  14.              //增加一列,默认可以不加
  15.             excel.CreateColumn(5, 100);
  16.              //新增表头行
  17.             excel.CreateRow();
  18.             excel.CreateCellString( "Name" );
  19.             excel.CreateCellString( "Score1" );
  20.             excel.CreateCellString( "Score1" );
  21.             excel.CreateCellString( "Score0" );
  22.             excel.CreateCellString( "说明" );
  23.              //新增两行数据
  24.             excel.CreateRow();
  25.             excel.CreateCellString( "jinjazz" );
  26.             excel.CreateCellNumber(100);
  27.             excel.CreateCellNumber(98);
  28.             excel.CreateCell(0,  "Number" "RC[-2]+RC[-1]" ,1,1);  //公式,-2和-1代表当前cell的水平偏移量
  29.             excel.CreateCell(0,  "String" "RC[-4]&/":/"&RC[-1]" , 1, 1); //公式
  30.             excel.CreateRow();
  31.             excel.CreateCellString( "游客" );
  32.             excel.CreateCellNumber(33);
  33.             excel.CreateCellNumber(14);
  34.             excel.CreateCell(0,  "Number" "RC[-2]+RC[-1]" , 1, 1);
  35.             excel.CreateCell(0,  "String" "RC[-4]&/":/"&RC[-1]" , 1, 1);
  36.              //新增汇总行
  37.             excel.CreateRow();
  38.             excel.CreateCellString( "总计" );
  39.             excel.CreateCell(0,  "Number" "SUM(R[-2]C:R[-1]C)" , 1, 1); //公式,-2和-1代表cell的垂直偏移量
  40.             excel.CreateCell(0,  "Number" "SUM(R[-2]C:R[-1]C)" , 1, 1);
  41.             excel.CreateCell(0,  "Number" "SUM(R[-2]C:R[-1]C)" , 1, 1);
  42.              //增加三个空行
  43.             excel.CreateRow();
  44.             excel.CreateRow();
  45.             excel.CreateRow();
  46.              //增加一个合并过的单元格
  47.             excel.CreateCell( "http://blog.csdn.net/jinjazz" , "String" , null ,2,5);
  48.             excel.Save(@ "c:/testData.xls" );
  49.         }
  50.     }
  51.      public   class  ExcelWriter
  52.     {
  53.          string  ssns =  "urn:schemas-microsoft-com:office:spreadsheet" ;
  54.          string  xmlns =  "urn:schemas-microsoft-com:office:spreadsheet" ;
  55.         XmlDocument _doc =  new  XmlDocument();
  56.         XmlNode _currentSheet =  null ;
  57.         XmlNode _currentRow =  null ;
  58.         
  59.          public  ExcelWriter()
  60.         {
  61.              //excel的xml模版,你需要了解xml的Attributes怎么用
  62.             StringBuilder sbody =  new  StringBuilder();
  63.             sbody.Append( "<?xml version=/"1.0/"?>/n" );
  64.             sbody.Append( "<?mso-application progid=/"Excel.Sheet/"?>/n" );
  65.             sbody.Append( "<Workbook xmlns=/"urn:schemas-microsoft-com:office:spreadsheet/"/n" );
  66.             sbody.Append( "xmlns:o=/"urn:schemas-microsoft-com:office:office/"/n" );
  67.             sbody.Append( "xmlns:x=/"urn:schemas-microsoft-com:office:excel/"/n" );
  68.             sbody.Append( "xmlns:ss=/"urn:schemas-microsoft-com:office:spreadsheet/"/n" );
  69.             sbody.Append( "xmlns:html=/"http://www.w3.org/TR/REC-html40/">/n" );
  70.             sbody.Append( "<Styles>/n" );
  71.             sbody.Append( "<Style ss:ID=/"Default/" ss:Name=/"Normal/">/n" );
  72.             sbody.Append( "<Alignment ss:Vertical=/"Center/"/>/n" );
  73.             sbody.Append( "<Borders/>/n" );
  74.             sbody.Append( "<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"10/"/>/n" );
  75.             sbody.Append( "<Interior/>/n" );
  76.             sbody.Append( "<NumberFormat/>/n" );
  77.             sbody.Append( "<Protection/>/n" );
  78.             sbody.Append( "</Style>/n" );
  79.             sbody.Append( "</Styles>/n" );
  80.             sbody.Append( "</Workbook>/n" );
  81.             _doc.LoadXml(sbody.ToString());
  82.         }
  83.          /// <summary>
  84.          /// 增加一个工作表
  85.          /// </summary>
  86.          /// <param name="sheetName">工作表名称</param>
  87.          public   void  CreateSheet( string  sheetName)
  88.         {
  89.             System.Xml.XmlNode node = _doc.CreateNode(XmlNodeType.Element,  "Worksheet" , ssns);
  90.             System.Xml.XmlAttribute xa = _doc.CreateAttribute( "ss" "Name" , xmlns);
  91.             xa.Value = sheetName;
  92.             node.Attributes.Append(xa);
  93.             _doc.ChildNodes[2].AppendChild(node);
  94.             node.AppendChild(_doc.CreateNode(XmlNodeType.Element,  "Table" , xmlns));
  95.             _currentSheet = node;
  96.         }
  97.          /// <summary>
  98.          /// 增加一行
  99.          /// </summary>
  100.          public   void  CreateRow()
  101.         {
  102.             System.Xml.XmlNode node = _doc.CreateNode(XmlNodeType.Element,  "Row" , xmlns);
  103.             _currentSheet.ChildNodes[0].AppendChild(node);
  104.             _currentRow = node;
  105.         }
  106.          /// <summary>
  107.          /// 增加一列
  108.          /// </summary>
  109.          /// <param name="index">索引</param>
  110.          /// <param name="width">宽度</param>
  111.          public   void  CreateColumn( int  index, float  width)
  112.         {
  113.             System.Xml.XmlNode node = _doc.CreateNode(XmlNodeType.Element,  "Column" , xmlns);
  114.             System.Xml.XmlAttribute xa = _doc.CreateAttribute( "ss" "Index" , xmlns);
  115.             xa.Value = index.ToString();
  116.             node.Attributes.Append(xa);
  117.             xa = _doc.CreateAttribute( "ss" "Width" , xmlns);
  118.             xa.Value = width.ToString();
  119.             node.Attributes.Append(xa);
  120.             _currentSheet.ChildNodes[0].AppendChild(node);
  121.         }
  122.          /// <summary>
  123.          /// 增加一个单元格
  124.          /// </summary>
  125.          /// <param name="value">值</param>
  126.          /// <param name="Type">类型</param>
  127.          /// <param name="Expression">公式</param>
  128.          /// <param name="rowSpan">跨行</param>
  129.          /// <param name="colSpan">跨列</param>
  130.          public   void  CreateCell( object  value,  string  Type,  string  Expression,  int  rowSpan,  int  colSpan)
  131.         {
  132.             System.Xml.XmlAttribute xa =  null ;
  133.             System.Xml.XmlNode nodeCell = _doc.CreateNode(XmlNodeType.Element,  "Cell" , xmlns);
  134.             _currentRow.AppendChild(nodeCell);
  135.              if  (! string .IsNullOrEmpty(Expression))
  136.             {
  137.                 xa = _doc.CreateAttribute( "ss" "Formula" , xmlns);
  138.                 xa.Value =  "="  + Expression;
  139.                 nodeCell.Attributes.Append(xa);
  140.             }
  141.              if  (--colSpan > 0)
  142.             {
  143.                 xa = _doc.CreateAttribute( "ss" "MergeAcross" , xmlns);
  144.                 xa.Value = colSpan.ToString();
  145.                 nodeCell.Attributes.Append(xa);
  146.             }
  147.              if  (--rowSpan > 0)
  148.             {
  149.                 xa = _doc.CreateAttribute( "ss" "MergeDown" , xmlns);
  150.                 xa.Value = rowSpan.ToString();
  151.                 nodeCell.Attributes.Append(xa);
  152.             }
  153.             System.Xml.XmlNode nodeData = _doc.CreateNode(XmlNodeType.Element,  "Data" , xmlns);
  154.             xa = _doc.CreateAttribute( "ss" "Type" , xmlns);
  155.             xa.Value = Type;
  156.             nodeData.Attributes.Append(xa);
  157.             nodeData.InnerText = value.ToString();
  158.             nodeCell.AppendChild(nodeData);
  159.         }
  160.          /// <summary>
  161.          /// 增加一个数字单元格
  162.          /// </summary>
  163.          /// <param name="value"></param>
  164.          public   void  CreateCellNumber( double  value)
  165.         {
  166.             CreateCell(value,  "Number" null , 1, 1);
  167.         }
  168.          /// <summary>
  169.          /// 增加一个字符串单元格
  170.          /// </summary>
  171.          /// <param name="value"></param>
  172.          public   void  CreateCellString( string  value)
  173.         {
  174.             CreateCell(value,  "String" null , 1, 1);
  175.         }
  176.          /// <summary>
  177.          /// 保存
  178.          /// </summary>
  179.          /// <param name="strFile"></param>
  180.          public   void  Save( string  strFile)
  181.         {
  182.             _doc.Save(strFile);
  183.         }
  184.     }
  185.     
  186. }

上面代码基本都是对xml文件的操作,需要你对xml的dom对象比较熟悉,尤其是Attributes的使用。

 

相关文章

通过文件结构直接生成xls文件