POI Microsoft Yahei and other style operations

Poi operation excel style

Microsoft Yahei

 

 

[java] view plaincopy    
 print ?
  1. package  com.cloud.poi.utils;  
  2. import  java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStream;  
  6. import org.apache.poi.ss.usermodel.Cell;  
  7. import org.apache.poi.ss.usermodel.CellStyle;  
  8. import org.apache.poi.ss.usermodel.Row;  
  9. import org.apache.poi.ss.usermodel.Sheet;  
  10. import org.apache.poi.ss.usermodel.Workbook;  
  11. import org.apache.poi.xssf.usermodel.XSSFCell;  
  12. import org.apache.poi.xssf.usermodel.XSSFFont;  
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  14. /** 
  15.  * Poi operation excel style 
  16.  * @author Henry 
  17.  */  
  18. public class  PoiExcelStyleAlter {   
  19.     publicstaticvoid main(String[] args) throws IOException {    
  20.         String filePath = "d:/";  
  21.         String fileName = "test";  
  22.         String fileType = "xlsx";  
  23.         Workbook workbook =  new  XSSFWorkbook(); //Create a workbook  
  24.         Sheet sheet = workbook.createSheet("test");//创建sheet  
  25.         sheet.setColumnHidden( 2 true ); //Hide the first column -- excel will not display the C column  
  26.         Row row = sheet.createRow(1);  
  27.         Cell cell = row.createCell(1);  
  28.         CellStyle style = workbook.createCellStyle(); //Create a new style object  
  29.         style.setWrapText( true ); //Set automatic line wrapping  
  30.         XSSFFont font = (XSSFFont) workbook.createFont(); //Create a font object  
  31.         font.setFontName( "Microsoft Yahei" );  
  32.         style.setFont(font);  
  33.         cell.setCellStyle(style);  
  34.         style.setLocked( true ); //Set whether the style referenced by the cell is locked  
  35.         cell.setCellType(XSSFCell.CELL_TYPE_STRING); //Set the cell format to string  
  36.         cell.setCellValue( "test I'm very tired now, you know, I'm really tired. I'm very tired, very tired, very tired! ~I Tiger YOU!" );  
  37.         File file = new File(filePath + fileName + "." + fileType);  
  38.         OutputStream stream = new FileOutputStream(file);  
  39.         workbook.write(stream);  
  40.         stream.close();  
  41.     }  
  42. }  

Transfer: http://blog.csdn.net/lihaiyun718/article/details/8197691

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326846911&siteId=291194637