我正在使用 C# 控制台应用程序,我想创建一个 Excel 文件并将两行文本插入 Excel 中的单个单元格。我想在我的 C# 程序的 Excel 单元格中插入换行符,以便在单个单元格中输出,例如:
this is line 1 text
this is line 2 text
我尝试使用
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";
还有这个
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";
但对于两者来说,它都会在单元格顶部添加一些额外的空间。即使我删除文本换行并且只使用\n
or Environment.NewLine
,它仍然会在单元格顶部添加这个额外的空间。
我在 C# 中的完整代码如下所示 - 我正在使用该gembox.spreadsheet
库
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();
输出是