C# 文本操作

Excel转Json

导入EPPlus插件
引用命名空间:OfficeOpenXml、Newtonsoft.Json
示例代码:

public static void ConvertExcelToJson(string excelFilePath, string jsonFilePath)
{
    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

    FileInfo excelFile = new FileInfo(excelFilePath);
    if (!excelFile.Exists)
    {
        Console.WriteLine("Excel file does not exist.");
        return;
    }

    using (var package = new ExcelPackage(excelFile))
    {
        var worksheet = package.Workbook.Worksheets[0];
        var rows = worksheet.Cells;
        var rowCount = worksheet.Dimension.End.Row;
        var columnCount = worksheet.Dimension.End.Column;

        List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();

        // Assuming the first row contains column headers
        for (int row = 2; row <= rowCount; row++)
        {
            if (string.IsNullOrEmpty(worksheet.GetValue<string>(row, 1)))
                continue;

            Dictionary<string, string> rowData = new Dictionary<string, string>();
            for (int col = 1; col <= columnCount; col++)
            {
                if (string.IsNullOrEmpty(worksheet.GetValue<string>(1, col)))
                    continue;

                string header = worksheet.Cells[1, col].Text;
                string cellValue = worksheet.Cells[row, col].Text;
                rowData[header] = cellValue;
            }
            data.Add(rowData);
        }

        string jsonData = JsonConvert.SerializeObject(data, Formatting.Indented);

        jsonFilePath = $"{Path.GetDirectoryName(excelFilePath)}\\{Path.GetFileNameWithoutExtension(excelFilePath)}_{worksheet.Name}.json";
        File.WriteAllText(jsonFilePath, jsonData);
        Console.WriteLine("Excel data has been converted to JSON and saved to " + jsonFilePath);
    }
}

Excel转Xml

导入EPPlus插件
引用命名空间:OfficeOpenXml、System.Xml
示例代码:

public static void ConvertExcelToXml(string excelFilePath, string xmlFilePath)
{
    using (var package = new ExcelPackage(new FileInfo(excelFilePath)))
    {
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

        var worksheet = package.Workbook.Worksheets[0];
        var rowCount = worksheet.Dimension.End.Row;
        var columnCount = worksheet.Dimension.End.Column;

        var xmlDoc = new XmlDocument();
        var rootElement = xmlDoc.CreateElement("Data");
        xmlDoc.AppendChild(rootElement);

        for (int row = 2; row <= rowCount; row++)
        {
            if (string.IsNullOrEmpty(worksheet.GetValue<string>(row, 1)))
                continue;

            var rowElement = xmlDoc.CreateElement("Row");
            rootElement.AppendChild(rowElement);

            for (int col = 1; col <= columnCount; col++)
            {
                if (string.IsNullOrEmpty(worksheet.GetValue<string>(1, col)))
                    continue;

                var cellKey = worksheet.Cells[1, col].Text;
                var cellValue = worksheet.Cells[row, col].Text;
                var cellElement = xmlDoc.CreateElement(cellKey);
                cellElement.InnerText = cellValue;
                rowElement.AppendChild(cellElement);
            }
        }

        xmlFilePath = $"{Path.GetDirectoryName(excelFilePath)}\\{Path.GetFileNameWithoutExtension(excelFilePath)}_{worksheet.Name}.xml";

        xmlDoc.Save(xmlFilePath);
    }
}

其他待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值