C# 实现XML和Excel相互转换
时间: 2023-11-26 14:42:22 浏览: 93
C# 读取XML文件并写入Excel表格
5星 · 资源好评率100%
你可以使用 C# 中的 `OpenXML` 库来实现 XML 和 Excel 文件之间的相互转换。下面是一个简单的示例代码,演示了如何将 XML 数据导出到 Excel 文件,并将 Excel 文件导入为 XML 数据。
首先,你需要添加对 `DocumentFormat.OpenXml` 和 `WindowsBase` 的引用。然后,你可以尝试以下代码:
```csharp
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
public static class XmlExcelConverter
{
public static void ExportXmlToExcel(string xmlFilePath, string excelFilePath)
{
XDocument xmlDocument = XDocument.Load(xmlFilePath);
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(excelFilePath, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
sheets.Append(sheet);
var data = xmlDocument.Descendants("data").ToList();
for (int i = 0; i < data.Count; i++)
{
var rowData = data[i].Descendants("row").ToList();
for (int j = 0; j < rowData.Count; j++)
{
var cellData = rowData[j].Descendants("cell").ToList();
for (int k = 0; k < cellData.Count; k++)
{
var cell = new Cell() { DataType = CellValues.String, CellValue = new CellValue(cellData[k].Value) };
worksheetPart.Worksheet.GetFirstChild<SheetData>().AppendChild(new Row(new Cell[] { cell }));
}
}
}
workbookPart.Workbook.Save();
}
}
public static void ImportExcelToXml(string excelFilePath, string xmlFilePath)
{
XDocument xmlDocument = new XDocument();
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(excelFilePath, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
var rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
XElement rootElement = new XElement("root");
foreach (var row in rows)
{
XElement rowData = new XElement("row");
foreach (var cell in row.Elements<Cell>())
{
var cellValue = GetCellValue(cell, workbookPart);
XElement cellData = new XElement("cell", cellValue);
rowData.Add(cellData);
}
rootElement.Add(rowData);
}
xmlDocument.Add(rootElement);
}
xmlDocument.Save(xmlFilePath);
}
private static string GetCellValue(Cell cell, WorkbookPart workbookPart)
{
if (cell.DataType == null)
{
return cell.CellValue.InnerText;
}
string value = cell.CellValue.InnerText;
if (cell.DataType.Value == CellValues.SharedString)
{
var sharedStringTablePart = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (sharedStringTablePart != null)
{
return sharedStringTablePart.SharedStringTable.ChildElements[int.Parse(value)].InnerText;
}
}
return value;
}
}
```
使用此代码,你可以将指定的 XML 文件导出到 Excel 文件中,或将 Excel 文件导入为 XML 文件。例如:
```csharp
string xmlFilePath = "path/to/xml/file.xml";
string excelFilePath = "path/to/excel/file.xlsx";
// 将 XML 导出为 Excel
XmlExcelConverter.ExportXmlToExcel(xmlFilePath, excelFilePath);
// 将 Excel 导入为 XML
XmlExcelConverter.ImportExcelToXml(excelFilePath, xmlFilePath);
```
请确保指定的 XML 文件和 Excel 文件存在,并根据实际情况修改文件路径。希望这可以帮助到你!
阅读全文