https://docs.aspose.com/display/cellsnet/AutoFit%2BRows%2Band%2BColumns
缩小到适合
在字段中包装文本的选项是缩小文本大小以适合单元格的尺寸。 这是通过将Style对象的IsTextWrapped属性设置为true来完成的 。
介绍文档
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.ShrinkToFit = true;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);
Examples-CSharp-Formatting-ConfiguringAlignmentSettings-ShrinkingToFit-1.cs hosted with ❤ by GitHub
自动换行和缩小字体来适应列宽是2个不同的概念吧,缩小字体来适应列宽可以用Spire.XLS (Excel类库)来实现
http://www.e-iceblue.cn/Introduce/Free-Spire-XLS-NET.html
//加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");
//获取第一张工作表
Worksheet sheet = workbook.Worksheets[0];
//指定需要自动缩小字体的单元格范围
CellRange cell = sheet.Range["A1:E3"];
//设置ShrinkToFit为true来缩小字体
CellStyle style = cell.Style;
style.ShrinkToFit = true;
//保存文档
workbook.Save();