【发布时间】:2021-06-16 13:38:22
【问题描述】:
我正在使用 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 或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();
输出是
【问题讨论】:
-
是文本内容中的空格(你能把它擦掉吗?)还是只是行高?
-
你确定这是你的完整代码吗?如果您只运行该代码,您会重现此问题吗?从屏幕截图中,我看到您的列“A”没有默认宽度,但您的 sn-p 代码没有设置它,所以我认为您没有显示所有内容。您是否有可能在某处明确设置该行的高度?
标签: c# excel gembox-spreadsheet