简体   繁体   English

使用C#和OpenXML写入xlsx文件时,会修剪Excel单元格文本

[英]Excel cell text is trimmed when writen in xlsx file when using C# & OpenXML

When writing inside an excel file the text in the cell gets trimmed after writing. 在excel文件中写入时,写入后会修剪单元格中的文本。

For example: 例如:
Let's say I am trying to write : "\\r\\nThis text starts with a new line " I would expect when I open the xlsx file for the cell to start with an empty line, but instead I get this "This text starts with a new line" 假设我要写: "\\r\\nThis text starts with a new line "我期望在打开单元格的xlsx文件时以空行开头,但我却得到了这样的信息"This text starts with a new line"

This happens only to the whitespace at the beginning and the end, which essentially gets trimmed. 这仅在开始和结尾的空白处发生,而空白基本上已被修剪。

I have tried setting the cell format like this but it doesn't seem to work: 我尝试过像这样设置单元格格式,但似乎不起作用:

new CellFormat()
{
    ApplyAlignment = true,
    Alignment = new Alignment
    {
        WrapText = new BooleanValue(false)
    }
}

The problem is that the underlying format is XML. 问题在于基础格式是XML。 Any whitespace at the start or end of a value is ignored unless you mark the space as preserved. 除非将空格标记为保留,否则将忽略值开头或结尾的所有空格。

To do this you need to set the Space property to SpaceProcessingModeValues.Preserve , for example: 为此,您需要将Space属性设置为SpaceProcessingModeValues.Preserve ,例如:

Cell cell = new Cell() { CellReference = "A1" };
CellValue value = new CellValue("\r\nThis text starts with a new line");
value.Space = SpaceProcessingModeValues.Preserve;
cell.CellValue = value;
cell.DataType = new EnumValue<CellValues>(CellValues.String);

This produces XML that looks like: 这样生成的XML看起来像:

<x:c r="A1" t="str">
    <x:v xml:space="preserve">
This text starts with a new line</x:v>
</x:c>

The xml:space="preserve" will prevent the whitespace from being removed from the beginning or end of the value. xml:space="preserve"将防止从值的开头或结尾删除空格。 This produces a file that looks like the below where you can see the newline is preserved. 这将产生一个如下所示的文件,您可以看到其中保留了换行符。

显示换行符的Excel输出将保留。

而不是同时添加\\r\\n ,请尝试仅添加\\r

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ExcelLibrary 在 C# 中读取 Excel .xlsx 文件时出现 OutOfMemoryException - OutOfMemoryException when reading Excel .xlsx file in C# using ExcelLibrary 使用DocumentFormat.OpenXML创建的Excel文件(.xlsx)在Excel打开时需要修复 - Excel file (.xlsx) created by using DocumentFormat.OpenXML needs to be repaired when opening in Excel 如何使用带有c#的openxml(不是整个行/列)隐藏和保护excel文件中一个Cell的文本? - How can I hide and protect the text of one Cell in an excel file using openxml with c# (not the entire row/ column)? 如何在 C# 中使用 OpenXML 将 RTF/HTML 放入 Excel 单元格 - How to put RTF/HTML into Excel cell using OpenXML in C# 如何使用带有c#的OpenXML SDK v2.0将新工作表添加到Excel .xlsx文件中? - How do I add a new sheet to an Excel .xlsx file using the OpenXML SDK v2.0 with c#? 使用 OpenXML C# 获取 Excel 中单元格的列索引 - Get the Column Index of a Cell in Excel using OpenXML C# 在Excel文件中搜索字符串,并在C#中使用OpenXML返回搜索到的单元格的rowindex和columnindex - Search string in excel file and return searched cell's rowindex and columnindex using OpenXML in C# 在C#中使用OpenXml在Excel中创建日期单元格中的问题 - Issue in Creating Date cell in Excel using OpenXml in C# 在创建Excel文件时,如何使用OpenXml向单元格添加文本换行? - How do you add text wrapping to a cell using OpenXml when creating excel files? "使用 OpenXML 和 C# 在 Excel 文档中设置文本居中对齐" - Set text align to center in an Excel document using OpenXML with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM