excel到XML – 显示一些错误纠正我\

我有3个excel文件,每个excel文件包含一定数量的数据。每个excel文件都join一列,这里我的问题是想从excel文件中获得一个xml文件。

首先Excel工作表包含以下信息 – excel名称 – 员工

emp id || empname || compid || departid

500 || xxx || 01 || 一个

501 || yyy || 02 || B

第二张excel表格包含以下信息 – excel名称 – 公司

Compid || compname || Location

01 || YYY || Ktpna

02 || 02-YYY || Thdpza

第三个excel表单包含以下信息 – excel名称 – 部门

Departid || Departname || 位置|| 队长

A || A-AAA || A-Ktpna || A-XXX

B || B-AAA || B-Thdpza || B-YYY

我想得到一个XMl输出

<employeesInformation> <Employees> <emp id> 500 </emp id> <empname> xxx </empname> <compid> 01 </compid> <departid> A </departid> </Employees> <Company> <Compid> 01 </Compid> <compname> YYY </compname> <Location> Ktpna </Location> </Company> <Department> <Departid >A<Departid> <Departname> A-AAA <Departname> <Location>A-Ktpna<Location> <Teamleader> A-XXX<Teamleader> </Department> <employeesInformation> <employeesInformation> <Employees> <emp id> 501 </emp id> <empname> yyy </empname> <compid> 02 </compid> <departid> B </departid> </Employees> <Company> <Compid> 02 </Compid> <compname> 02-YYY </compname> <Location> Thdpza </Location> </Company> <Department> <Departid >B<Departid> <Departname> B-AAA <Departname> <Location>B-Thdpza <Location> <Teamleader> B-YYY<Teamleader> </Department> <employeesInformation> 

并使用以下代码

  private DataTable ReadExcelFile(string filename) { // Initialize an instance of DataTable DataTable dt = new DataTable(); try { // Use SpreadSheetDocument class of Open XML SDK to open excel file using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false)) { // Get Workbook Part of Spread Sheet Document WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; // Get all sheets in spread sheet document IEnumerable<Sheet> sheetcollection = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>(); // Get relationship Id string relationshipId = sheetcollection.First().Id.Value; // Get sheet1 Part of Spread Sheet Document WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId); // Get Data in Excel file SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First(); IEnumerable<Row> rowcollection = sheetData.Descendants<Row>(); if (rowcollection.Count() == 0) { return dt; } // Add columns foreach (Cell cell in rowcollection.ElementAt(0)) { dt.Columns.Add(GetValueOfCell(spreadsheetDocument, cell)); } // Add rows into DataTable foreach (Row row in rowcollection) { DataRow temprow = dt.NewRow(); int columnIndex = 0; foreach (Cell cell in row.Descendants<Cell>()) { // Get Cell Column Index int cellColumnIndex = GetColumnIndex(GetColumnName(cell.CellReference)); if (columnIndex < cellColumnIndex) { do { temprow[columnIndex] = string.Empty; columnIndex++; } while (columnIndex < cellColumnIndex); } temprow[columnIndex] = GetValueOfCell(spreadsheetDocument, cell); columnIndex++; } // Add the row to DataTable // the rows include header row dt.Rows.Add(temprow); } } // Here remove header row dt.Rows.RemoveAt(0); return dt; } catch (IOException ex) { throw new IOException(ex.Message); } }