Excel表格导出成XML文件

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Xml;

using System.Reflection;

using ExcelTools = Microsoft.Office.Tools.Excel;

using Excel = Microsoft.Office.Interop.Excel;

 

namespace etox

{

class Program

{

static void Main(string[] args)

{

try

{

//读取Excel

//Console.WriteLine("start");

//FileStream FS = new FileStream(@"C:/a.xls", FileMode.Open);

Excel.Application app = new Excel.Application();

object Unknown = System.Type.Missing;

Excel.Workbook rwb = app.Workbooks.Open(@"C:/b.xls", Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);

Excel.Worksheet rs = rwb.Worksheets["联系人"] as Excel.Worksheet;

int l = rs.UsedRange.Columns.Count;

int w = rs.UsedRange.Rows.Count;

//Console.WriteLine("{0}", l);

//Console.WriteLine("{0}", w);

 

//Xml操作

XmlDocument doc = new XmlDocument();

doc.AppendChild(doc.CreateElement("", "allmetadata", ""));

XmlElement root = doc.DocumentElement;

 

object[,] dell = rs.UsedRange.get_Value(Missing.Value) as object[,];

//Console.WriteLine("{0}", dell[1, 1].ToString());

string str = "";

 

for (int i = 2; i < w; i++)

{

XmlElement instment = doc.CreateElement("", "联系人" + (i - 1).ToString(), "");

for (int k = 1; k < l; k++)

{

if (dell[i, k] != null)

{

str = dell[i, k].ToString();

}

else

{

str = "";

}

XmlElement e = doc.CreateElement("", dell[1, k].ToString(), "");

XmlText XT = doc.CreateTextNode(str);

e.AppendChild(XT);

instment.AppendChild(e);

e = null;

}

root.AppendChild(instment);

instment = null;

}

XmlWriter outputter = XmlWriter.Create("lishewen.xml");

doc.WriteContentTo(outputter);

outputter.Close();

app.Quit();

}

catch (Exception e)

{

Console.Write(e.Message);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值