2013-05-16 50 views
3

我正在使用OPENXML SDK 2.0來傳輸電子表格文件。源數據來自數據表並使用openxml將其寫入Spreadsheet。如果有一個數據表的列數據中有一個具有「Treshold%」(此文本上有標籤空間)並且寫入excel相同,但是將它寫入到「Treshold%」 in excel單元格和刪除標籤空間。OpenXML電子表格 - 在寫入單元格值時在值之前或之後保留空格

我使用的代碼如下。使用workSheetWriter.PasteText和方法。

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

請幫忙。如何將開始的tab空間值(Treshold%)寫入excel單元格的格式爲相同的格式?

回答

3

我使用OPENXML SDK 2.0

由於沒有workSheetWriter的OpenXML SDK 2.0,我猜你是使用這個庫:Simple OOXML

我不使用此庫,但是,

我覺得你不能通過這些方法,因爲.PastText.PasteValue方法使用CellValues.String被存儲文本和CellValue,這將導致該空間將被忽略:

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

通過使用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); 
相關問題