using System; using System.Text; using System.IO; using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using NPOI.SS.UserModel; namespace SetWidthAndHeightInXls { class Program { static void Main(string[] args) { InitializeWorkbook(); Sheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); //set the width of columns sheet1.SetColumnWidth(0,50 * 256); sheet1.SetColumnWidth(1, 100 * 256); sheet1.SetColumnWidth(2, 150 * 256); //set the width of height sheet1.CreateRow(0).Height = 100*20; sheet1.CreateRow(1).Height = 200*20; sheet1.CreateRow(2).Height = 300*20; sheet1.DefaultRowHeightInPoints = 50; WriteToFile(); } static HSSFWorkbook hssfworkbook; static void WriteToFile() { //Write the stream data of workbook to the root directory FileStream file = new FileStream(@"test.xls", FileMode.Create); hssfworkbook.Write(file); file.Close(); } static void InitializeWorkbook() { hssfworkbook = new HSSFWorkbook(); //create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; //create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; } } } 下面一个范例是自动边宽AutoSizeColumn using System; using System.Text; using System.IO; using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using NPOI.SS.UserModel; namespace AutoSizeColumnInXls { class Program { static void Main(string[] args) { InitializeWorkbook(); Sheet sheet=hssfworkbook.CreateSheet("Sheet1"); Row row=sheet.CreateRow(0); row.CreateCell(0).SetCellValue("This is a test"); row.CreateCell(1).SetCellValue("Hello"); row.CreateCell(2).SetCellValue("1234.0023"); sheet.AutoSizeColumn(0); sheet.AutoSizeColumn(1); sheet.AutoSizeColumn(2); WriteToFile(); } static HSSFWorkbook hssfworkbook; static void WriteToFile() { //Write the stream data of workbook to the root directory FileStream file = new FileStream(@"test.xls", FileMode.Create); hssfworkbook.Write(file); file.Close(); } static void InitializeWorkbook() { hssfworkbook = new HSSFWorkbook(); //create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; //create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; } } }