5

我正在使用 OPENXML SDK 2.0 流式传输电子表格文件。源数据来自数据表,并使用 openxml 将其写入电子表格。如果数据表的某一列数据具有“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);
}

请帮助解决这个问题。如何将开头的选项卡空间(Treshold%)的值以相同的格式写入excel单元格?

4

1 回答 1

6

I am using OPENXML SDK 2.0

Since there is no workSheetWriter class in OpenXML SDK 2.0, I guess you are using this library: Simple OOXML

I don't use this library but,

I think you can't by using these methods, since the .PastText and .PasteValue methods are storing text by using CellValues.String and CellValue, and this causes that the space is ignored:

{
    cell.CellValue = new CellValue(value);
    cell.DataType = new EnumValue<CellValues>(type);
}

By using OPENXML SDK 2.0 only, I am able to accomplish what you want (preserve space) by using CellValues.InlineString type, InlineString and Text class:

Text text1 = new Text
{
    Text = " Text with space at beginning",
    Space = SpaceProcessingModeValues.Preserve
};

cell.InlineString = new InlineString(text1);
cell.DataType = new EnumValue<CellValues>(CellValues.InlineString);
于 2013-05-18T22:01:18.037 回答