OpenXML Spreadsheet-在写入单元格值时保留值之前或之后的空间

use*_*252 5 c# openxml spreadsheetml openxml-sdk

我正在使用OPENXML SDK 2.0来传输电子表格文件.源数据来自数据表,并使用openxml将其写入Spreadsheet.如果数据表的列数据中有一个具有"Treshold%"(此文本上面有选项卡空间)并且同样写入excel但是在excel单元格中将其写入"Treshold%"并删除标签空间.

我使用的代码如下.使用workSheetWriter.PasteTextworkSheetWriter.PasteValue方法.

WorksheetWriter workSheetWriter = new WorksheetWriter(spreadSheet, workSheet);

int intValue = 0;
if (strValue.Contains("$"))
{
    strValue = strValue.Replace("$", "");
    strValue = strValue.Replace(",", "");

    workSheetWriter.PasteValue(cellLocation, strValue, CellValues.Number);
}
else if (int.TryParse(strValue, out intValue))
{
    workSheetWriter.PasteValue(cellLocation, strValue, CellValues.Number);
}
else if (string.IsNullOrEmpty(strValue))
{
    workSheetWriter.PasteText(cellLocation, strValue);
}
else
{
    workSheetWriter.PasteText(cellLocation, strValue);
}
Run Code Online (Sandbox Code Playgroud)

请帮忙.如何将开头的标签空间(Treshold%)中的值写入excel单元格中的格式相同?

Chr*_*ris 6

我正在使用OPENXML SDK 2.0

由于OpenXML SDK 2.0中没有workSheetWriter类,我猜你正在使用这个库:简单的OOXML

我不使用这个库,但是,

我认为你不能通过使用这些方法,因为.PastText.PasteValue方法是使用CellValues.String和存储文本CellValue,这导致空间被忽略:

{
    cell.CellValue = new CellValue(value);
    cell.DataType = new EnumValue<CellValues>(type);
}
Run Code Online (Sandbox Code Playgroud)

通过仅使用OPENXML SDK 2.0,我可以通过使用CellValues.InlineString类型InlineStringText类来完成您想要的(保留空间):

Text text1 = new Text() {  space = SpaceProcessingModeValues.Preserve};
text1.Text = " Text with space at beginning";
cell.InlineString = new InlineString(text1);
cell.DataType = new EnumValue<CellValues>(CellValues.InlineString);
Run Code Online (Sandbox Code Playgroud)