I am using a C# console application and I want to create an Excel file and insert two lines of text into a single cell in Excel. I want to insert new line character in an Excel cell from my C# program so that I have output in single cell like:
this is line 1 text
this is line 2 text
I tried using
worksheet.Cells[0, 0].Value = "Components";
worksheet.Rows[1].Cells[0].Style.WrapText = true;
worksheet.Cells[1, 0].Value = "this is line 1 and "+Environment.NewLine+" this is line 2";
and also this
worksheet.Cells[0, 0].Value = "Components";
worksheet.Rows[1].Cells[0].Style.WrapText = true;
worksheet.Cells[1, 0].Value = "this is line 1 and \n this is line 2";
but for both it add some extra space to the top of cell. Even if I remove text wrap and only use \n
or Environment.NewLine
, it still adds this extra space to top of cell.
My complete code in C# is shown below - I am using the gembox.spreadsheet
library
using GemBox.Spreadsheet;
// If you are using the Professional version, enter your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
// Create empty Excel file with a sheet
ExcelFile workbook = new ExcelFile();
ExcelWorksheet worksheet = workbook.Worksheets.Add("Issues");
worksheet.Cells[0, 0].Value = "Components";
worksheet.Rows[1].Cells[0].Style.WrapText = true;
worksheet.Cells[1, 0].Value = "this is line 1 and "+Environment.NewLine+" this is line 2";
// Save excel file
workbook.Save("E:/.net projects/CSharpToExcel/JsonToExcel.xlsx");
Console.WriteLine("data inserted successfully");
Console.ReadKey();
The output is