How to insert programmatically a new line in an Excel cell in C#?

后端 未结 13 1062
情话喂你
情话喂你 2020-12-30 00:32

I\'m using the Aspose library to create an Excel document. Somewhere in some cell I need to insert a new line between two parts of the text.

I tried \"\\r\\n\" but i

相关标签:
13条回答
  • 2020-12-30 01:01

    SpreadsheetGear for .NET does it this way:

            IWorkbook workbook = Factory.GetWorkbook();
            IRange a1 = workbook.Worksheets[0].Cells["A1"];
            a1.Value = "Hello\r\nWorld!";
            a1.WrapText = true;
            workbook.SaveAs(@"c:\HelloWorld.xlsx", FileFormat.OpenXMLWorkbook);
    

    Note the "WrapText = true" - Excel will not wrap the text without this. I would assume that Aspose has similar APIs.

    Disclaimer: I own SpreadsheetGear LLC

    0 讨论(0)
  • 2020-12-30 01:03

    Using PEAR 'Spreadsheet_Excel_Writer' and 'OLE':

    Only way I could get "\n" to work was making the cell $format->setTextWrap(); and then using "\n" would work.

    0 讨论(0)
  • 2020-12-30 01:05
    E.Run runForBreak = new E.Run();
    
    E.Text textForBreak = new E.Text() { Space = SpaceProcessingModeValues.Preserve };
    textForBreak.Text = "\n";
    runForBreak.Append(textForBreak);
    sharedStringItem.Append(runForBreak);
    
    0 讨论(0)
  • 2020-12-30 01:06

    "\n" works fine. If the input is coming from a multi-line textbox, the new line characters will be "\r\n", if this is replaced with "\n", it will work.

    0 讨论(0)
  • 2020-12-30 01:06

    You can use Chr(13). Then just wrap the whole thing in Chr(34). Chr(34) is double quotes.

    • VB.Net Example:
    • TheContactInfo = ""
    • TheContactInfo = Trim(TheEmail) & chr(13)
    • TheContactInfo = TheContactInfo & Trim(ThePhone) & chr(13)
    • TheContactInfo = Chr(34) & TheContactInfo & Chr(34)
    0 讨论(0)
  • 2020-12-30 01:08

    Actually, it is really simple.

    You may edit an xml version of excel. Edit a cell to give it new line between your text, then save it. Later you may open the file in editor, then you will see a new line is represented by 


    Have a try....

    0 讨论(0)
提交回复
热议问题