Poi operation excel style
Microsoft Yahei
- package com.cloud.poi.utils;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFFont;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- /**
- * Poi operation excel style
- * @author Henry
- */
- public class PoiExcelStyleAlter {
- publicstaticvoid main(String[] args) throws IOException {
- String filePath = "d:/";
- String fileName = "test";
- String fileType = "xlsx";
- Workbook workbook = new XSSFWorkbook(); //Create a workbook
- Sheet sheet = workbook.createSheet("test");//创建sheet
- sheet.setColumnHidden( 2 , true ); //Hide the first column -- excel will not display the C column
- Row row = sheet.createRow(1);
- Cell cell = row.createCell(1);
- CellStyle style = workbook.createCellStyle(); //Create a new style object
- style.setWrapText( true ); //Set automatic line wrapping
- XSSFFont font = (XSSFFont) workbook.createFont(); //Create a font object
- font.setFontName( "Microsoft Yahei" );
- style.setFont(font);
- cell.setCellStyle(style);
- style.setLocked( true ); //Set whether the style referenced by the cell is locked
- cell.setCellType(XSSFCell.CELL_TYPE_STRING); //Set the cell format to string
- 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!" );
- File file = new File(filePath + fileName + "." + fileType);
- OutputStream stream = new FileOutputStream(file);
- workbook.write(stream);
- stream.close();
- }
- }
Transfer: http://blog.csdn.net/lihaiyun718/article/details/8197691