在Openxml中使用SharedStringTablePart储存文字信息比较麻烦。在实际操作过程中使用InlineString是个不错的选择。在下面的例子中我将演示如何使用InlineString。
另外在这个示例中,为了实现line break的效果,我设置了单无格的自动换行。这个也可能是最简的自动换行的设置。我把它放在这里供大家参考。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using log4net; using System.Windows.Forms; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; namespace ForumIssue0002 { public class LineBreakAndInlinestring { private ILog _log = log4net.LogManager.GetLogger(typeof(LineBreakAndInlinestring)); private OpenFileDialog _OFD = null; private SaveFileDialog _SFD = null; private string _TargetPath = string.Empty; private string _OriginalPath = string.Empty; public void Init() { _TargetPath = "Test.xlsx"; } public void Go() { using (SpreadsheetDocument SSD = SpreadsheetDocument .Create(_TargetPath, SpreadsheetDocumentType.Workbook)) { WorkbookPart WBP = SSD.AddWorkbookPart(); Workbook WB = new Workbook(); WorksheetPart WSP = WBP.AddNewPart<WorksheetPart>(); string rId = WBP.GetIdOfPart(WSP); WBP.Workbook = WB; Sheets Ss = new Sheets(); Sheet S = new Sheet() { Id = rId, SheetId = (UInt32Value)1U, Name = "Test Sheet" }; Ss.Append(S); WB.Append(Ss); /*------------------------------------------------------------*/ /* 下面的代码是设定工作薄样式,Font, Fill, Border是必须的。否则 * 程序会崩溃。 /*------------------------------------------------------------*/ WorkbookStylesPart WBSP = WBP.AddNewPart<WorkbookStylesPart>(); Stylesheet SS = new Stylesheet(); Fonts Fs = new Fonts(); Font F = new Font(); Fs.Append(F); SS.Append(Fs); Fills Fis = new Fills(); Fill Fi = new Fill(); Fis.Append(Fi); SS.Append(Fis); Borders Bs = new Borders(); Border B = new Border(); Bs.Append(B); SS.Append(Bs); CellFormats CFs = new CellFormats(); CellFormat CF = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }; CellFormat CF1 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }; // 设置自动换行为“True” Alignment A = new Alignment() { WrapText = true }; CF1.Append(A); CFs.Append(CF); CFs.Append(CF1); SS.Append(CFs); WBSP.Stylesheet = SS; /*-------------------------------------------------------------- * 下面是工作薄的设定,在设定中我们将使用InlineString来储存文字。 *------------------------------------------------------------*/ Worksheet WS = new Worksheet(); SheetData SD = new SheetData(); Row R = new Row() { RowIndex = (UInt32Value)1U, CustomFormat = true }; Cell C = new Cell() { CellReference = "A1", DataType = CellValues.InlineString, /* * InlineString的使用方法 */ InlineString = new InlineString() { Text = new Text() { // “\n”代表换行(line break)在没有自动换行的情况下我们 // 看不到效果 Text = "This is a link\nbreak test" } }, // 引用样式实现自动换行 StyleIndex = (UInt32Value)1U }; R.Append(C); SD.Append(R); Columns Cs = new Columns(); Column Co = new Column() { Min = (UInt32Value)1U, Max = (UInt32Value)1U, Width = 30.5703125D, // 自动调整列宽 BestFit = true, CustomWidth = true, Style = (UInt32Value)1U }; Cs.Append(Co); WS.Append(Cs); WS.Append(SD); WSP.Worksheet = WS; WS.Save(); } } } }