執行 AutoFitColumns 後,Column的字還是沒有完全呈現出來

您好,
我透過 Aspose.Cells 匯入 DataTable 的資料,如下,

protected void btnAutoFits_Click(object sender, EventArgs e)
{
	//output file name
	var folderName = Server.MapPath("./data");
	var fileNameWithoutExt = $"{Guid.NewGuid().ToString("N")}";
	var outputExcel = Path.Combine(folderName, $"{fileNameWithoutExt}.xlsx");
	var dt = GetDataSource();

	//proc excel
	// Instantiating a Workbook object            
	var workbook = new Workbook();
	var worksheet = workbook.Worksheets[0];
	worksheet.Cells.ImportDataTable(dt, true, "A1");
	var range = worksheet.Cells.MaxDisplayRange;
	var pageSetup = workbook.Worksheets[0].PageSetup;
	//var titleEndColumnName = CellsHelper.ColumnIndexToName(range.ColumnCount-1);
	//pageSetup.PrintTitleColumns = $"$A:${titleEndColumnName}";
	pageSetup.PrintTitleRows = "$1:$1";
	pageSetup.IsPercentScale = true;

	worksheet.AutoFitColumns();
	worksheet.AutoFitRows();
	// Saving the Excel file
	workbook.Save(outputExcel);
}
private DataTable GetDataSource()
{
	// Instantiating a "Products" DataTable object
	var dataTable = new DataTable("Products");
	// Adding columns to the DataTable object
	dataTable.Columns.Add("ProductID", typeof(Int32));
	dataTable.Columns.Add("ProductName", typeof(string));
	dataTable.Columns.Add("ProductDesc", typeof(string));
	dataTable.Columns.Add("Units", typeof(Double));
	var rand = new Random();
	for (var i = 0; i < 90; i++)
	{
		dataTable.Rows.Add(i, $"產品名稱-{i} extend 中文長度分制 讓它長一點 ", $"產品描述 -{i} extend 中文長度分制 讓它長一點 extend ... ABC 中文 描述 ... 中文 ", rand.NextDouble());
	}
	return dataTable;
}

輸出的檔案如附件。
autofitcolumns.zip (152.6 KB)

@rainmaker_ho,

感谢模板文件,示例代码和屏幕截图。

初步测试后,我可以通过将示例代码与模板文件一起使用来观察问题。 我发现在执行Worksheet.AutoFitColumns之后,列中的单词仍未完全显示。

我为您的问题录制了一张ID为“CELLSNET-46218”的机票。 我们很快就会调查。

一旦我们更新了它,我们将在此通知您。

1 Like

@rainmaker_ho

我们调查了CELLSNET-46218及以下是与记录问题相关的调查结果。

默认字体是Arial,但Arial字体无法在“ProductName”和“ProductDesc”列中呈现文本。 因此,Aspose.Cells和Microsoft Excel会在自动调整列时找到其他字体来呈现文本,但Aspose.Cells和Microsoft Excel可能会使用不同的字体,因此结果可能会有所不同。 更改为可以在“ProductName”和“ProductDesc”列中呈现文本的字体(例如“Arial Unicode MS”)将获得良好的结果。

请在您的结尾使用以下代码段。

        var dt = GetDataSource();

        //proc excel
        // Instantiating a Workbook object
        var workbook = new Workbook();

        Style style = workbook.DefaultStyle;
        style.Font.Name = "Arial Unicode MS";
        workbook.DefaultStyle = style;

        var worksheet = workbook.Worksheets[0];
        worksheet.Cells.ImportDataTable(dt, true, "A1");
        var range = worksheet.Cells.MaxDisplayRange;
        var pageSetup = workbook.Worksheets[0].PageSetup;
        //var titleEndColumnName = CellsHelper.ColumnIndexToName(range.ColumnCount-1);
        //pageSetup.PrintTitleColumns = $"$A:${titleEndColumnName}";
        pageSetup.PrintTitleRows = "$1:$1";
        pageSetup.IsPercentScale = true;

        worksheet.AutoFitColumns();
        worksheet.AutoFitRows();
        // Saving the Excel file
        workbook.Save(outputExcel);
1 Like

您好,
我設定了 Font.Name = “Arial Unicode MS” 它還是無法正確調整 column width
於是我設定 style.IsTextWrapped = true; ,並將DataTable 的欄位名稱設定成中文,如下,

private DataTable GetDataSource()
{
	// Instantiating a "Products" DataTable object
	var dataTable = new DataTable("Products");
	// Adding columns to the DataTable object
	dataTable.Columns.Add("產品\n代號", typeof(Int32));
	dataTable.Columns.Add("ProductName", typeof(string));
	dataTable.Columns.Add("產品\n描述", typeof(string));
	dataTable.Columns.Add("Units", typeof(Double));
	var rand = new Random();
	for (var i = 0; i < 90; i++)
	{
		dataTable.Rows.Add(i, $"產品名稱-{i} extend 中文長度分制 讓它長一點 ", $"產品描述 -{i} extend 中文長度分制 讓它長一點 extend ... ABC 中文 描述 ... 中文 ", rand.NextDouble());
	}
	return dataTable;
}

private string test(DataTable dt)
{
	var folderName = Server.MapPath("./data");
	//output file name
	var fileNameWithoutExt = $"{Guid.NewGuid().ToString("N")}";
	var outputExcel = Path.Combine(folderName, $"{fileNameWithoutExt}_tmp.xlsx");
	var tempPdf = Path.Combine(folderName, $"{fileNameWithoutExt}_tmp.pdf");
	var outputPdf = Path.Combine(folderName, $"{fileNameWithoutExt}.pdf");
	//proc excel
	// Instantiating a Workbook object            
	var workbook = new Workbook();
	var wkstyle = workbook.DefaultStyle;
	wkstyle.Font.Name = "Arial Unicode MS";
	workbook.DefaultStyle = wkstyle;

	var worksheet = workbook.Worksheets[0];
	worksheet.Cells.ImportDataTable(dt, true, "A1");

	var range = worksheet.Cells.MaxDisplayRange;
	var pageSetup = workbook.Worksheets[0].PageSetup;
	//var titleEndColumnName = CellsHelper.ColumnIndexToName(range.ColumnCount-1);
	//pageSetup.PrintTitleColumns = $"$A:${titleEndColumnName}";
	pageSetup.PrintTitleRows = "$1:$1";
	pageSetup.IsPercentScale = true;
	pageSetup.SetFitToPages(1, 0);


	//border
	//Setting border for each cell in the range
	var style = workbook.CreateStyle();
	var colorBlack = System.Drawing.Color.Black;
	style.SetBorder(BorderType.BottomBorder, CellBorderType.Medium, colorBlack);
	style.SetBorder(BorderType.LeftBorder, CellBorderType.Medium, colorBlack);
	style.SetBorder(BorderType.RightBorder, CellBorderType.Medium, colorBlack);
	style.SetBorder(BorderType.TopBorder, CellBorderType.Medium, colorBlack);
	style.IsTextWrapped = true;
	style.Font.Name = "Arial Unicode MS";
	range.SetStyle(style);
	worksheet.AutoFitColumns();
	worksheet.AutoFitRows();
	workbook.Save(outputExcel);
	return outputExcel;
}

// Call test method
test(GetDataSource());

透過 AutoFitColumns() 後,第一個欄位寬度會變很小,導致第一列的高度變高,第一個欄位原本應該是 產品\n代號 ,如附件,
autocolumns.zip (9.5 KB)
感謝您的幫忙

@rainmaker_ho

我使用了更新的代码片段,也可以正常工作。 附加输出文件以供参考。 似乎“Arial Unicode MS”可能未安装在您的系统上。 请确保您的环境中安装了“Arial Unicode MS”字体。 它适用于style.IsTextWrapped = false。temp.zip

您好,
我 Search Windows/fonts 目錄 的確找不到 Arial Unicode MS 字型,我改成 Microsoft JhengHei Light 字型後,運行的確是很好,謝謝您

另外,如果要讓某個 cell 支持換行的話,是否一定要將 IsTextWrapped 設定為 true 才行呢? 謝謝您

@rainmaker_ho,

感谢您告诉我们您已解决此问题。

是的,要将单元格中的文本换行到多行,需要将IsTextWrapped属性设置为true。

The issues you have found earlier (filed as CELLSNET-46218) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by Amjad_Sahi

1 Like