此文章转自:http://www.cnblogs.com/scdsun/archive/2007/03/27/681326.html
程序所实现的功能是从一个表示成员信息的xml文档中读取数据,并将这些数据导出为Excel表格,表格的样式如下图所示:
表示成员信息的xml文档(保存在c:/members.xml供下面的代码调用)内容如下:
张三 男 1980年12月4日 汉 党员 江苏南京 本科 025-8000000 138138***** 南京大学 南京市中山东路******* [email protected] 李四 男 1981年9月12日 汉 团员 江苏苏州 本科 025-8000000 138138***** 东南大学 南京市某某路******* [email protected] 小美 女 1982年1月18日 汉 团员 安徽黄山 本科 025-8000000 138138***** 南京理工大学 南京市某某路******* [email protected] 王二 男 1981年4月14日 汉 团员 山东烟台 研究生 025-8000000 138138***** 南京航空航天大学 南京市某某路******* [email protected] 小莉 女 1982年3月18日 汉 党员 江苏常熟 研究生 025-8000000 138138***** 南京大学 南京市某某路******* [email protected] Jack 男 1980年12月4日 汉 党员 江苏南京 本科 025-8000000 138138***** 南京大学 南京市中山东路******* [email protected] Joe 男 1981年9月12日 汉 团员 江苏苏州 本科 025-8000000 138138***** 东南大学 南京市某某路******* [email protected] Katty 女 1982年1月18日 汉 团员 安徽黄山 本科 025-8000000 138138***** 南京理工大学 南京市某某路******* [email protected] Scott 男 1981年4月14日 汉 团员 山东烟台 研究生 025-8000000 138138***** 南京航空航天大学 南京市某某路******* [email protected] Lily 女 1982年3月18日 汉 党员 江苏常熟 研究生 025-8000000 138138***** 南京大学 南京市某某路******* [email protected]
[xml] view plain copy print ?
- 程序使用.net 2.0/c#开发,代码(省略Design部分代码):
程序使用.net 2.0/c#开发,代码(省略Design部分代码):
[xml] view plain copy print ?
- <pre class="csharp" name="code"><pre class="csharp" name="code">using System;
-
- using System.Collections.Generic;
-
- using System.ComponentModel;
-
- using System.Data;
-
- using System.Drawing;
-
- using System.Text;
-
- using System.Windows.Forms;
-
- using System.Xml;
-
-
-
- namespace ExportWPS
-
- {
-
- public partial class Form1 : Form
-
- {
-
- ET.Application objApp = null;
-
- ET._Workbook objWorkBook = null;
-
- ET.Worksheet objWorkSheet = null;
-
-
-
- public Form1()
-
- {
-
- InitializeComponent();
-
- }
-
-
-
- private void button1_Click(object sender, EventArgs e)
-
- {
-
- DateTime dt = DateTime.Now;
-
-
-
- try
-
- {
-
- objApp = new ET.Application();
-
-
-
- XmlDocument doc = new XmlDocument();
-
- doc.Load(@"c:/members.xml");
-
-
-
- XmlElement root = doc.DocumentElement;
-
- XmlNodeList nodes = root.SelectNodes("member");
-
-
-
-
-
- foreach (XmlElement memberElement in nodes)
-
- {
-
- ExportMember(memberElement);
-
- }
-
- }
-
- finally
-
- {
-
- ReleaseComObject(objApp);
-
- }
-
-
-
- TimeSpan ts = DateTime.Now - dt;
-
- MessageBox.Show(ts.ToString());
-
- }
-
-
-
- private void ExportMember(XmlElement memberElement)
-
- {
-
- object missing = Type.Missing;
-
- ET.Range objRange = null;
-
-
-
- try
-
- {
-
-
-
- objWorkBook = objApp.Workbooks.Add(Type.Missing);
-
- objWorkSheet = objWorkBook.ActiveSheet;
-
-
-
- //设置标题
-
- objWorkSheet.get_Range("A1", "G1").Merge(true); //先进行单元合并
-
- objRange = objWorkSheet.get_Range("A1", "A1");
-
- objRange.Value2 = "表 格 示 例"; //设置合并后的单元格的文本
-
- objRange.RowHeight = 40; //设置行高
-
- //设置字体
-
- objRange.Font.Name = "宋体";
-
- objRange.Font.Size = 20;
-
- objRange.Font.Bold = true;
-
- objRange.HorizontalAlignment = ET.ETHAlign.etHAlignCenter; //设置字体对其方向
-
-
-
- objRange = objWorkSheet.get_Range("A2", "G7");
-
- objRange.Borders.LineStyle = ET.ETLineStyle.etContinuous;
-
- objRange.Borders.Weight = ET.ETBorderWeight.etThin;
-
- objRange.RowHeight = 17;
-
- objRange.Borders.Color = 0; //设置边框的颜色,颜色的值可以按照B,G,R的方式合成
-
- //设置表格的外边框,加粗
-
- objRange.Borders[ET.ETBorderIndex.etEdgeBottom].Weight = ET.ETBorderWeight.etMedium;
-
- objRange.Borders[ET.ETBorderIndex.etEdgeLeft].Weight = ET.ETBorderWeight.etMedium;
-
- objRange.Borders[ET.ETBorderIndex.etEdgeRight].Weight = ET.ETBorderWeight.etMedium;
-
- objRange.Borders[ET.ETBorderIndex.etEdgeTop].Weight = ET.ETBorderWeight.etMedium;
-
- objRange.HorizontalAlignment = ET.ETHAlign.etHAlignCenter;
-
- objRange.Font.Name = "宋体";
-
- objRange.Font.Size = 10;
-
-
-
- string name = memberElement.SelectSingleNode("name").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("A2", "A2").Value2 = "姓 名";
-
- objWorkSheet.get_Range("A2", "A2").ColumnWidth = 60;
-
-
-
- objWorkSheet.get_Range("B2", "B2").ColumnWidth = 75;
-
- objWorkSheet.get_Range("B2", "B2").Value2 = name;
-
-
-
- objWorkSheet.get_Range("C2", "C2").Value2 = "性 别";
-
- objWorkSheet.get_Range("C2", "C2").ColumnWidth = 60;
-
-
-
- objWorkSheet.get_Range("D2", "D2").ColumnWidth = 75;
-
- objWorkSheet.get_Range("D2", "D2").Value2 = memberElement.SelectSingleNode("sex").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("E2", "E2").Value2 = "出生年月";
-
- objWorkSheet.get_Range("E2", "E2").ColumnWidth = 60;
-
-
-
- objWorkSheet.get_Range("F2", "F2").ColumnWidth = 75;
-
- objWorkSheet.get_Range("F2", "F2").Value2 = memberElement.SelectSingleNode("birthday").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("G2", "G7").Merge(false);
-
- objWorkSheet.get_Range("G2", "G2").ColumnWidth = 80;
-
- objWorkSheet.get_Range("G2", "G2").Value2 = "照片";
-
-
-
- objWorkSheet.get_Range("A3", "A3").Value2 = "民 族";
-
- objWorkSheet.get_Range("B3", "B3").Value2 = memberElement.SelectSingleNode("nationality").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("C3", "C3").Value2 = "政治面貌";
-
- objWorkSheet.get_Range("D3", "D3").Value2 = memberElement.SelectSingleNode("political_status").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("E3", "E3").Value2 = "籍 贯";
-
- objWorkSheet.get_Range("F3", "F3").Value2 = memberElement.SelectSingleNode("native_place").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("A4", "A4").Value2 = "学 历";
-
- objWorkSheet.get_Range("B4", "B4").Value2 = memberElement.SelectSingleNode("qualification").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("C4", "C4").Value2 = "固定电话";
-
- objWorkSheet.get_Range("D4", "D4").Value2 = memberElement.SelectSingleNode("telephone").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("E4", "E4").Value2 = "移动电话";
-
- objWorkSheet.get_Range("F4", "F4").Value2 = memberElement.SelectSingleNode("mobile").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("A5", "A5").Value2 = "毕业院校";
-
- objWorkSheet.get_Range("B5", "F5").Merge(false);
-
- objWorkSheet.get_Range("B5", "B5").Value2 = memberElement.SelectSingleNode("graduating_school").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("A6", "A6").Value2 = "家庭住址";
-
- objWorkSheet.get_Range("B6", "F6").Merge(false);
-
- objWorkSheet.get_Range("B6", "B6").Value2 = memberElement.SelectSingleNode("address").ChildNodes[0].Value;
-
-
-
- objWorkSheet.get_Range("A7", "A7").Value2 = "Email";
-
- objWorkSheet.get_Range("B7", "F7").Merge(false);
-
- objWorkSheet.get_Range("B7", "B7").Value2 = memberElement.SelectSingleNode("email").ChildNodes[0].Value;
-
-
-
- objWorkBook.SaveAs(string.Format("c://{0}.xls", name), missing, missing, missing, missing, missing, ET.ETSaveAsAccessMode.etExclusive, missing, missing, missing, missing);
-
- objWorkBook.Close(missing, missing, missing);
-
- }
-
- finally
-
- {
-
- ReleaseComObject(objRange);
-
- ReleaseComObject(objWorkSheet);
-
- ReleaseComObject(objWorkBook);
-
- }
-
- }
-
-
-
- private void ReleaseComObject(object obj)
-
- {
-
- if (obj != null)
-
- {
-
- System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
-
- obj = null;
-
- }
-
- }
-
- }
-
- }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; namespace ExportWPS { public partial class Form1 : Form { ET.Application objApp = null; ET._Workbook objWorkBook = null; ET.Worksheet objWorkSheet = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DateTime dt = DateTime.Now; try { objApp = new ET.Application(); XmlDocument doc = new XmlDocument(); doc.Load(@"c:/members.xml"); XmlElement root = doc.DocumentElement; XmlNodeList nodes = root.SelectNodes("member"); foreach (XmlElement memberElement in nodes) { ExportMember(memberElement); } } finally { ReleaseComObject(objApp); } TimeSpan ts = DateTime.Now - dt; MessageBox.Show(ts.ToString()); } private void ExportMember(XmlElement memberElement) { object missing = Type.Missing; ET.Range objRange = null; try { objWorkBook = objApp.Workbooks.Add(Type.Missing); objWorkSheet = objWorkBook.ActiveSheet; //设置标题 objWorkSheet.get_Range("A1", "G1").Merge(true); //先进行单元合并 objRange = objWorkSheet.get_Range("A1", "A1"); objRange.Value2 = "表 格 示 例"; //设置合并后的单元格的文本 objRange.RowHeight = 40; //设置行高 //设置字体 objRange.Font.Name = "宋体"; objRange.Font.Size = 20; objRange.Font.Bold = true; objRange.HorizontalAlignment = ET.ETHAlign.etHAlignCenter; //设置字体对其方向 objRange = objWorkSheet.get_Range("A2", "G7"); objRange.Borders.LineStyle = ET.ETLineStyle.etContinuous; objRange.Borders.Weight = ET.ETBorderWeight.etThin; objRange.RowHeight = 17; objRange.Borders.Color = 0; //设置边框的颜色,颜色的值可以按照B,G,R的方式合成 //设置表格的外边框,加粗 objRange.Borders[ET.ETBorderIndex.etEdgeBottom].Weight = ET.ETBorderWeight.etMedium; objRange.Borders[ET.ETBorderIndex.etEdgeLeft].Weight = ET.ETBorderWeight.etMedium; objRange.Borders[ET.ETBorderIndex.etEdgeRight].Weight = ET.ETBorderWeight.etMedium; objRange.Borders[ET.ETBorderIndex.etEdgeTop].Weight = ET.ETBorderWeight.etMedium; objRange.HorizontalAlignment = ET.ETHAlign.etHAlignCenter; objRange.Font.Name = "宋体"; objRange.Font.Size = 10; string name = memberElement.SelectSingleNode("name").ChildNodes[0].Value; objWorkSheet.get_Range("A2", "A2").Value2 = "姓 名"; objWorkSheet.get_Range("A2", "A2").ColumnWidth = 60; objWorkSheet.get_Range("B2", "B2").ColumnWidth = 75; objWorkSheet.get_Range("B2", "B2").Value2 = name; objWorkSheet.get_Range("C2", "C2").Value2 = "性 别"; objWorkSheet.get_Range("C2", "C2").ColumnWidth = 60; objWorkSheet.get_Range("D2", "D2").ColumnWidth = 75; objWorkSheet.get_Range("D2", "D2").Value2 = memberElement.SelectSingleNode("sex").ChildNodes[0].Value; objWorkSheet.get_Range("E2", "E2").Value2 = "出生年月"; objWorkSheet.get_Range("E2", "E2").ColumnWidth = 60; objWorkSheet.get_Range("F2", "F2").ColumnWidth = 75; objWorkSheet.get_Range("F2", "F2").Value2 = memberElement.SelectSingleNode("birthday").ChildNodes[0].Value; objWorkSheet.get_Range("G2", "G7").Merge(false); objWorkSheet.get_Range("G2", "G2").ColumnWidth = 80; objWorkSheet.get_Range("G2", "G2").Value2 = "照片"; objWorkSheet.get_Range("A3", "A3").Value2 = "民 族"; objWorkSheet.get_Range("B3", "B3").Value2 = memberElement.SelectSingleNode("nationality").ChildNodes[0].Value; objWorkSheet.get_Range("C3", "C3").Value2 = "政治面貌"; objWorkSheet.get_Range("D3", "D3").Value2 = memberElement.SelectSingleNode("political_status").ChildNodes[0].Value; objWorkSheet.get_Range("E3", "E3").Value2 = "籍 贯"; objWorkSheet.get_Range("F3", "F3").Value2 = memberElement.SelectSingleNode("native_place").ChildNodes[0].Value; objWorkSheet.get_Range("A4", "A4").Value2 = "学 历"; objWorkSheet.get_Range("B4", "B4").Value2 = memberElement.SelectSingleNode("qualification").ChildNodes[0].Value; objWorkSheet.get_Range("C4", "C4").Value2 = "固定电话"; objWorkSheet.get_Range("D4", "D4").Value2 = memberElement.SelectSingleNode("telephone").ChildNodes[0].Value; objWorkSheet.get_Range("E4", "E4").Value2 = "移动电话"; objWorkSheet.get_Range("F4", "F4").Value2 = memberElement.SelectSingleNode("mobile").ChildNodes[0].Value; objWorkSheet.get_Range("A5", "A5").Value2 = "毕业院校"; objWorkSheet.get_Range("B5", "F5").Merge(false); objWorkSheet.get_Range("B5", "B5").Value2 = memberElement.SelectSingleNode("graduating_school").ChildNodes[0].Value; objWorkSheet.get_Range("A6", "A6").Value2 = "家庭住址"; objWorkSheet.get_Range("B6", "F6").Merge(false); objWorkSheet.get_Range("B6", "B6").Value2 = memberElement.SelectSingleNode("address").ChildNodes[0].Value; objWorkSheet.get_Range("A7", "A7").Value2 = "Email"; objWorkSheet.get_Range("B7", "F7").Merge(false); objWorkSheet.get_Range("B7", "B7").Value2 = memberElement.SelectSingleNode("email").ChildNodes[0].Value; objWorkBook.SaveAs(string.Format("c://{0}.xls", name), missing, missing, missing, missing, missing, ET.ETSaveAsAccessMode.etExclusive, missing, missing, missing, missing); objWorkBook.Close(missing, missing, missing); } finally { ReleaseComObject(objRange); ReleaseComObject(objWorkSheet); ReleaseComObject(objWorkBook); } } private void ReleaseComObject(object obj) { if (obj != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } } } }
[csharp] view plain copy print ?
-
[csharp] view plain copy print ?
- 程序执行后,在c:/下生成10份xls文档。并且提示执行导出代码所使用的时间
程序执行后,在c:/下生成10份xls文档。并且提示执行导出代码所使用的时间
[csharp] view plain copy print ?
- "" src="https://p-blog.csdn.net/images/p_blog_csdn_net/chaobeyond/2.jpg" />
[csharp] view plain copy print ?
- 总共花费5.5秒,应该说这个时间还是完全可以接受的。另外,导出的文件可以完全兼容MS Excel 2003