在Excel使用当中,当单元格的文本超过该列宽度时,希望文本能自动换行显示,Spire.XLS for .NET是一款类似Excel的文档处理控件,完全使用代码实现Excel的功能,该控件可以帮助开发人员轻松实现单元格文本的自动换行显示,具体如下:
using Spire.XLS;
namespace wrap
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook;
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Wrap the excel text;
sheet.Range["C1"].Text = "e-iceblue is in facebook and welcome to like us";
sheet.Range["C1"].Style.WrapText = true;
sheet.Range["D1"].Text = "e-iceblue is in twitter and welcome to follow us";
sheet.Range["D1"].Style.WrapText= true;
//Unwrap the excel text;
sheet.Range["C2"].Text = "http://www.facebook.com/pages/e-iceblue/139657096082266";
sheet.Range["C2"].Style.WrapText= false;
sheet.Range["D2"].Text = "https://twitter.com/eiceblue";
sheet.Range["D2"].Style.WrapText = false;
//set the text color of Range["C1:D1"]
sheet.Range["C1:D1"].Style.Font.Size = 15;
sheet.Range["C1:D1"].Style.Font.Color = Color.Blue;
//set the text color of Range["C2:D2"]
sheet.Range["C2:D2"].Style.Font.Size = 15;
sheet.Range["C2:D2"].Style.Font.Color = Color.DeepSkyBlue;
// Save the project;
workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start(workbook.FileName);
}
}
}