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);
}
}
}
}