C#中导出数据到WPS表格或者Excel2003

此文章转自:http://www.cnblogs.com/scdsun/archive/2007/03/27/681326.html

程序所实现的功能是从一个表示成员信息的xml文档中读取数据,并将这些数据导出为Excel表格,表格的样式如下图所示:

表示成员信息的xml文档(保存在c:/members.xml供下面的代码调用)内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2.  
  3. <members> 
  4.  
  5.     <member> 
  6.  
  7.         <name>张三</name> 
  8.  
  9.         <sex></sex> 
  10.  
  11.         <birthday>1980年12月4日</birthday> 
  12.  
  13.         <nationality></nationality> 
  14.  
  15.         <political_status>党员</political_status> 
  16.  
  17.         <native_place>江苏南京</native_place> 
  18.  
  19.         <qualification>本科</qualification> 
  20.  
  21.         <telephone>025-8000000</telephone> 
  22.  
  23.         <mobile>138138*****</mobile> 
  24.  
  25.         <graduating_school>南京大学</graduating_school> 
  26.  
  27.         <address>南京市中山东路*******</address> 
  28.  
  29.         <email>zhang.san@sample.com</email> 
  30.  
  31.     </member> 
  32.  
  33.     <member> 
  34.  
  35.         <name>李四</name> 
  36.  
  37.         <sex></sex> 
  38.  
  39.         <birthday>1981年9月12日</birthday> 
  40.  
  41.         <nationality></nationality> 
  42.  
  43.         <political_status>团员</political_status> 
  44.  
  45.         <native_place>江苏苏州</native_place> 
  46.  
  47.         <qualification>本科</qualification> 
  48.  
  49.         <telephone>025-8000000</telephone> 
  50.  
  51.         <mobile>138138*****</mobile> 
  52.  
  53.         <graduating_school>东南大学</graduating_school> 
  54.  
  55.         <address>南京市某某路*******</address> 
  56.  
  57.         <email>li.si@sample.com</email> 
  58.  
  59.     </member> 
  60.  
  61.     <member> 
  62.  
  63.         <name>小美</name> 
  64.  
  65.         <sex></sex> 
  66.  
  67.         <birthday>1982年1月18日</birthday> 
  68.  
  69.         <nationality></nationality> 
  70.  
  71.         <political_status>团员</political_status> 
  72.  
  73.         <native_place>安徽黄山</native_place> 
  74.  
  75.         <qualification>本科</qualification> 
  76.  
  77.         <telephone>025-8000000</telephone> 
  78.  
  79.         <mobile>138138*****</mobile> 
  80.  
  81.         <graduating_school>南京理工大学</graduating_school> 
  82.  
  83.         <address>南京市某某路*******</address> 
  84.  
  85.         <email>xiao.mei@sample.com</email> 
  86.  
  87.     </member> 
  88.  
  89.     <member> 
  90.  
  91.         <name>王二</name> 
  92.  
  93.         <sex></sex> 
  94.  
  95.         <birthday>1981年4月14日</birthday> 
  96.  
  97.         <nationality></nationality> 
  98.  
  99.         <political_status>团员</political_status> 
  100.  
  101.         <native_place>山东烟台</native_place> 
  102.  
  103.         <qualification>研究生</qualification> 
  104.  
  105.         <telephone>025-8000000</telephone> 
  106.  
  107.         <mobile>138138*****</mobile> 
  108.  
  109.         <graduating_school>南京航空航天大学</graduating_school> 
  110.  
  111.         <address>南京市某某路*******</address> 
  112.  
  113.         <email>wan.er@sample.com</email> 
  114.  
  115.     </member> 
  116.  
  117.     <member> 
  118.  
  119.         <name>小莉</name> 
  120.  
  121.         <sex></sex> 
  122.  
  123.         <birthday>1982年3月18日</birthday> 
  124.  
  125.         <nationality></nationality> 
  126.  
  127.         <political_status>党员</political_status> 
  128.  
  129.         <native_place>江苏常熟</native_place> 
  130.  
  131.         <qualification>研究生</qualification> 
  132.  
  133.         <telephone>025-8000000</telephone> 
  134.  
  135.         <mobile>138138*****</mobile> 
  136.  
  137.         <graduating_school>南京大学</graduating_school> 
  138.  
  139.         <address>南京市某某路*******</address> 
  140.  
  141.         <email>xiao.li@sample.com</email> 
  142.  
  143.     </member> 
  144.  
  145.     <member> 
  146.  
  147.         <name>Jack</name> 
  148.  
  149.         <sex></sex> 
  150.  
  151.         <birthday>1980年12月4日</birthday> 
  152.  
  153.         <nationality></nationality> 
  154.  
  155.         <political_status>党员</political_status> 
  156.  
  157.         <native_place>江苏南京</native_place> 
  158.  
  159.         <qualification>本科</qualification> 
  160.  
  161.         <telephone>025-8000000</telephone> 
  162.  
  163.         <mobile>138138*****</mobile> 
  164.  
  165.         <graduating_school>南京大学</graduating_school> 
  166.  
  167.         <address>南京市中山东路*******</address> 
  168.  
  169.         <email>jack@sample.com</email> 
  170.  
  171.     </member> 
  172.  
  173.     <member> 
  174.  
  175.         <name>Joe</name> 
  176.  
  177.         <sex></sex> 
  178.  
  179.         <birthday>1981年9月12日</birthday> 
  180.  
  181.         <nationality></nationality> 
  182.  
  183.         <political_status>团员</political_status> 
  184.  
  185.         <native_place>江苏苏州</native_place> 
  186.  
  187.         <qualification>本科</qualification> 
  188.  
  189.         <telephone>025-8000000</telephone> 
  190.  
  191.         <mobile>138138*****</mobile> 
  192.  
  193.         <graduating_school>东南大学</graduating_school> 
  194.  
  195.         <address>南京市某某路*******</address> 
  196.  
  197.         <email>jeo@sample.com</email> 
  198.  
  199.     </member> 
  200.  
  201.     <member> 
  202.  
  203.         <name>Katty</name> 
  204.  
  205.         <sex></sex> 
  206.  
  207.         <birthday>1982年1月18日</birthday> 
  208.  
  209.         <nationality></nationality> 
  210.  
  211.         <political_status>团员</political_status> 
  212.  
  213.         <native_place>安徽黄山</native_place> 
  214.  
  215.         <qualification>本科</qualification> 
  216.  
  217.         <telephone>025-8000000</telephone> 
  218.  
  219.         <mobile>138138*****</mobile> 
  220.  
  221.         <graduating_school>南京理工大学</graduating_school> 
  222.  
  223.         <address>南京市某某路*******</address> 
  224.  
  225.         <email>Katty@sample.com</email> 
  226.  
  227.     </member> 
  228.  
  229.     <member> 
  230.  
  231.         <name>Scott</name> 
  232.  
  233.         <sex></sex> 
  234.  
  235.         <birthday>1981年4月14日</birthday> 
  236.  
  237.         <nationality></nationality> 
  238.  
  239.         <political_status>团员</political_status> 
  240.  
  241.         <native_place>山东烟台</native_place> 
  242.  
  243.         <qualification>研究生</qualification> 
  244.  
  245.         <telephone>025-8000000</telephone> 
  246.  
  247.         <mobile>138138*****</mobile> 
  248.  
  249.         <graduating_school>南京航空航天大学</graduating_school> 
  250.  
  251.         <address>南京市某某路*******</address> 
  252.  
  253.         <email>scott@sample.com</email> 
  254.  
  255.     </member> 
  256.  
  257.     <member> 
  258.  
  259.         <name>Lily</name> 
  260.  
  261.         <sex></sex> 
  262.  
  263.         <birthday>1982年3月18日</birthday> 
  264.  
  265.         <nationality></nationality> 
  266.  
  267.         <political_status>党员</political_status> 
  268.  
  269.         <native_place>江苏常熟</native_place> 
  270.  
  271.         <qualification>研究生</qualification> 
  272.  
  273.         <telephone>025-8000000</telephone> 
  274.  
  275.         <mobile>138138*****</mobile> 
  276.  
  277.         <graduating_school>南京大学</graduating_school> 
  278.  
  279.         <address>南京市某某路*******</address> 
  280.  
  281.         <email>Lily@sample.com</email> 
  282.  
  283.     </member> 
  284.  
  285. </members> 
  1. 程序使用.net 2.0/c#开发,代码(省略Design部分代码): 
  1. <pre class="csharp" name="code"><pre class="csharp" name="code">using System; 
  2.  
  3. using System.Collections.Generic; 
  4.  
  5. using System.ComponentModel; 
  6.  
  7. using System.Data; 
  8.  
  9. using System.Drawing; 
  10.  
  11. using System.Text; 
  12.  
  13. using System.Windows.Forms; 
  14.  
  15. using System.Xml; 
  16.  
  17.  
  18.  
  19. namespace ExportWPS 
  20.  
  21.  
  22.     public partial class Form1 : Form 
  23.  
  24.     { 
  25.  
  26.         ET.Application objApp = null
  27.  
  28.         ET._Workbook objWorkBook = null
  29.  
  30.         ET.Worksheet objWorkSheet = null
  31.  
  32.  
  33.  
  34.         public Form1() 
  35.  
  36.         { 
  37.  
  38.             InitializeComponent(); 
  39.  
  40.         } 
  41.  
  42.  
  43.  
  44.         private void button1_Click(object sender, EventArgs e) 
  45.  
  46.         { 
  47.  
  48.             DateTime dt = DateTime.Now; 
  49.  
  50.  
  51.  
  52.             try 
  53.  
  54.             { 
  55.  
  56.                 objApp = new ET.Application(); 
  57.  
  58.  
  59.  
  60.                 XmlDocument doc = new XmlDocument(); 
  61.  
  62.                 doc.Load(@"c:/members.xml"); 
  63.  
  64.  
  65.  
  66.                 XmlElement root = doc.DocumentElement; 
  67.  
  68.                 XmlNodeList nodes = root.SelectNodes("member"); 
  69.  
  70.  
  71.  
  72.  
  73.  
  74.                 foreach (XmlElement memberElement in nodes) 
  75.  
  76.                 { 
  77.  
  78.                     ExportMember(memberElement); 
  79.  
  80.                 } 
  81.  
  82.             } 
  83.  
  84.             finally 
  85.  
  86.             { 
  87.  
  88.                 ReleaseComObject(objApp); 
  89.  
  90.             } 
  91.  
  92.  
  93.  
  94.             TimeSpan ts = DateTime.Now - dt; 
  95.  
  96.             MessageBox.Show(ts.ToString()); 
  97.  
  98.         } 
  99.  
  100.  
  101.  
  102.         private void ExportMember(XmlElement memberElement) 
  103.  
  104.         { 
  105.  
  106.             object missing = Type.Missing; 
  107.  
  108.             ET.Range objRange = null
  109.  
  110.  
  111.  
  112.             try 
  113.  
  114.             { 
  115.  
  116.  
  117.  
  118.                 objWorkBook = objApp.Workbooks.Add(Type.Missing); 
  119.  
  120.                 objWorkSheet = objWorkBook.ActiveSheet; 
  121.  
  122.                  
  123.  
  124.                 //设置标题 
  125.  
  126.                 objWorkSheet.get_Range("A1", "G1").Merge(true);     //先进行单元合并 
  127.  
  128.                 objRange = objWorkSheet.get_Range("A1", "A1"); 
  129.  
  130.                 objRange.Value2 = "表 格 示 例";    //设置合并后的单元格的文本 
  131.  
  132.                 objRange.RowHeight = 40;        //设置行高 
  133.  
  134.                 //设置字体 
  135.  
  136.                 objRange.Font.Name = "宋体"
  137.  
  138.                 objRange.Font.Size = 20
  139.  
  140.                 objRange.Font.Bold = true
  141.  
  142.                 objRange.HorizontalAlignment = ET.ETHAlign.etHAlignCenter;  //设置字体对其方向 
  143.  
  144.  
  145.  
  146.                 objRange = objWorkSheet.get_Range("A2", "G7"); 
  147.  
  148.                 objRange.Borders.LineStyle = ET.ETLineStyle.etContinuous; 
  149.  
  150.                 objRange.Borders.Weight = ET.ETBorderWeight.etThin; 
  151.  
  152.                 objRange.RowHeight = 17
  153.  
  154.                 objRange.Borders.Color = 0;     //设置边框的颜色,颜色的值可以按照B,G,R的方式合成 
  155.  
  156.                 //设置表格的外边框,加粗 
  157.  
  158.                 objRange.Borders[ET.ETBorderIndex.etEdgeBottom].Weight = ET.ETBorderWeight.etMedium; 
  159.  
  160.                 objRange.Borders[ET.ETBorderIndex.etEdgeLeft].Weight = ET.ETBorderWeight.etMedium; 
  161.  
  162.                 objRange.Borders[ET.ETBorderIndex.etEdgeRight].Weight = ET.ETBorderWeight.etMedium; 
  163.  
  164.                 objRange.Borders[ET.ETBorderIndex.etEdgeTop].Weight = ET.ETBorderWeight.etMedium; 
  165.  
  166.                 objRange.HorizontalAlignment = ET.ETHAlign.etHAlignCenter; 
  167.  
  168.                 objRange.Font.Name = "宋体"
  169.  
  170.                 objRange.Font.Size = 10
  171.  
  172.  
  173.  
  174.                 string name = memberElement.SelectSingleNode("name").ChildNodes[0].Value; 
  175.  
  176.  
  177.  
  178.                 objWorkSheet.get_Range("A2", "A2").Value2 = "姓    名"
  179.  
  180.                 objWorkSheet.get_Range("A2", "A2").ColumnWidth = 60
  181.  
  182.  
  183.  
  184.                 objWorkSheet.get_Range("B2", "B2").ColumnWidth = 75
  185.  
  186.                 objWorkSheet.get_Range("B2", "B2").Value2 = name
  187.  
  188.  
  189.  
  190.                 objWorkSheet.get_Range("C2", "C2").Value2 = "性    别"
  191.  
  192.                 objWorkSheet.get_Range("C2", "C2").ColumnWidth = 60
  193.  
  194.  
  195.  
  196.                 objWorkSheet.get_Range("D2", "D2").ColumnWidth = 75
  197.  
  198.                 objWorkSheet.get_Range("D2", "D2").Value2 = memberElement.SelectSingleNode("sex").ChildNodes[0].Value; 
  199.  
  200.  
  201.  
  202.                 objWorkSheet.get_Range("E2", "E2").Value2 = "出生年月"
  203.  
  204.                 objWorkSheet.get_Range("E2", "E2").ColumnWidth = 60
  205.  
  206.  
  207.  
  208.                 objWorkSheet.get_Range("F2", "F2").ColumnWidth = 75
  209.  
  210.                 objWorkSheet.get_Range("F2", "F2").Value2 = memberElement.SelectSingleNode("birthday").ChildNodes[0].Value; 
  211.  
  212.  
  213.  
  214.                 objWorkSheet.get_Range("G2", "G7").Merge(false); 
  215.  
  216.                 objWorkSheet.get_Range("G2", "G2").ColumnWidth = 80
  217.  
  218.                 objWorkSheet.get_Range("G2", "G2").Value2 = "照片"
  219.  
  220.  
  221.  
  222.                 objWorkSheet.get_Range("A3", "A3").Value2 = "民    族"
  223.  
  224.                 objWorkSheet.get_Range("B3", "B3").Value2 = memberElement.SelectSingleNode("nationality").ChildNodes[0].Value; 
  225.  
  226.  
  227.  
  228.                 objWorkSheet.get_Range("C3", "C3").Value2 = "政治面貌"
  229.  
  230.                 objWorkSheet.get_Range("D3", "D3").Value2 = memberElement.SelectSingleNode("political_status").ChildNodes[0].Value; 
  231.  
  232.  
  233.  
  234.                 objWorkSheet.get_Range("E3", "E3").Value2 = "籍    贯"
  235.  
  236.                 objWorkSheet.get_Range("F3", "F3").Value2 = memberElement.SelectSingleNode("native_place").ChildNodes[0].Value; 
  237.  
  238.  
  239.  
  240.                 objWorkSheet.get_Range("A4", "A4").Value2 = "学    历"
  241.  
  242.                 objWorkSheet.get_Range("B4", "B4").Value2 = memberElement.SelectSingleNode("qualification").ChildNodes[0].Value; 
  243.  
  244.  
  245.  
  246.                 objWorkSheet.get_Range("C4", "C4").Value2 = "固定电话"
  247.  
  248.                 objWorkSheet.get_Range("D4", "D4").Value2 = memberElement.SelectSingleNode("telephone").ChildNodes[0].Value; 
  249.  
  250.  
  251.  
  252.                 objWorkSheet.get_Range("E4", "E4").Value2 = "移动电话"
  253.  
  254.                 objWorkSheet.get_Range("F4", "F4").Value2 = memberElement.SelectSingleNode("mobile").ChildNodes[0].Value; 
  255.  
  256.  
  257.  
  258.                 objWorkSheet.get_Range("A5", "A5").Value2 = "毕业院校"
  259.  
  260.                 objWorkSheet.get_Range("B5", "F5").Merge(false); 
  261.  
  262.                 objWorkSheet.get_Range("B5", "B5").Value2 = memberElement.SelectSingleNode("graduating_school").ChildNodes[0].Value; 
  263.  
  264.  
  265.  
  266.                 objWorkSheet.get_Range("A6", "A6").Value2 = "家庭住址"
  267.  
  268.                 objWorkSheet.get_Range("B6", "F6").Merge(false); 
  269.  
  270.                 objWorkSheet.get_Range("B6", "B6").Value2 = memberElement.SelectSingleNode("address").ChildNodes[0].Value; 
  271.  
  272.  
  273.  
  274.                 objWorkSheet.get_Range("A7", "A7").Value2 = "Email"
  275.  
  276.                 objWorkSheet.get_Range("B7", "F7").Merge(false); 
  277.  
  278.                 objWorkSheet.get_Range("B7", "B7").Value2 = memberElement.SelectSingleNode("email").ChildNodes[0].Value; 
  279.  
  280.  
  281.  
  282.                 objWorkBook.SaveAs(string.Format("c://{0}.xls", name), missing, missing, missing, missing, missing, ET.ETSaveAsAccessMode.etExclusive, missing, missing, missing, missing); 
  283.  
  284.                 objWorkBook.Close(missing, missing, missing); 
  285.  
  286.             } 
  287.  
  288.             finally 
  289.  
  290.             { 
  291.  
  292.                 ReleaseComObject(objRange); 
  293.  
  294.                 ReleaseComObject(objWorkSheet); 
  295.  
  296.                 ReleaseComObject(objWorkBook); 
  297.  
  298.             } 
  299.  
  300.         } 
  301.  
  302.  
  303.  
  304.         private void ReleaseComObject(object obj) 
  305.  
  306.         { 
  307.  
  308.             if (obj != null) 
  309.  
  310.             { 
  311.  
  312.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
  313.  
  314.                 obj = null
  315.  
  316.             } 
  317.  
  318.         } 
  319.  
  320.     } 
  321.  
  1. 程序执行后,在c:/下生成10份xls文档。并且提示执行导出代码所使用的时间 
  1. <img alt="" src="https://p-blog.csdn.net/images/p_blog_csdn_net/chaobeyond/2.jpg" /> 
  1. 总共花费5.5秒,应该说这个时间还是完全可以接受的。另外,导出的文件可以完全兼容MS Excel 2003 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值