public static DataSet ds = new DataSet();
//convert the xml file to dataset
public DataSet xmlToDataset(string xmlFullPath) {
XmlDocument xmldoc = null;
XmlTextReader reader=null;
StringReader stream=null;
try
{
xmldoc = new XmlDocument();
xmldoc.Load(xmlFullPath);
stream = new StringReader(xmldoc.InnerXml);
//load the stream to xmltextreader
reader = new XmlTextReader(stream);
ds.ReadXml(reader);
return ds;
}catch(Exception e){
throw new Exception("Load the xml file fail."+e.Message);
}
}
//convert the dataset to excel
public bool DataSetToExcel(DataSet ds) {
bool isSuccess = false;
if(ds==null){
return isSuccess;
}
//create excel
Application excel = new Application();
excel.Workbooks.Add(true);
excel.Visible = true;
int rowNum = ds.Tables[0].Rows.Count;
int colNum = ds.Tables[0].Columns.Count;
//get the column name
for(int i=0;i<colNum;i++){
excel.Cells[1, i+1] = ds.Tables[0].Columns[i].ColumnName;
}
//write the data
for (int i = 0; i < rowNum;i++ ) {
for (int j = 0; j < colNum;j++ ) {
excel.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
isSuccess = true;
return isSuccess;
}
本文介绍了一个将XML文件转换为DataSet的方法,并提供了一种将DataSet中的数据导出到Excel的工作流程。该方法首先加载XML文件并将其转换为XmlDocument对象,然后通过XmlTextReader读取并填充DataSet。此外,还实现了一个将DataSet中的数据写入Excel的功能。
136

被折叠的 条评论
为什么被折叠?



