【问题标题】:OpenXML Spreadsheet- Preserve space before or after the value when writing a cell valueOpenXML 电子表格 - 在写入单元格值时在值之前或之后保留空间
【发布时间】:2013-05-16 14:13:41
【问题描述】:

我正在使用 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单元格?

【问题讨论】:

    标签: c# openxml openxml-sdk spreadsheetml


    【解决方案1】:

    我正在使用 OPENXML SDK 2.0

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

    我不使用这个库,但是,

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

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

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

    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);
    

    【讨论】:

    • 如果有人想要另一个包含更多代码的示例:blogs.msdn.microsoft.com/wriju/2016/03/13/…。注意:此示例没有设置“保留”设置,这是保持间距所必需的。您需要修改“Text t = new Text();”行到“文本 t = new Text() { Space = SpaceProcessingModeValues.Preserve };”
    猜你喜欢
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多