iamlihongwei 2016-06-20
Poi 操作excel样式
微软雅黑 宋体
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 操作excel样式 * @author Henry */ public class PoiExcelStyleAlter { public static void main(String[] args) throws IOException { String filePath = "d:/"; String fileName = "test"; String fileType = "xlsx"; Workbook workbook = new XSSFWorkbook();//创建工作簿 Sheet sheet = workbook.createSheet("test");//创建sheet sheet.setColumnHidden(2, true);//隐藏第1列--excel就不显示C纵列了 Row row = sheet.createRow(1); Cell cell = row.createCell(1); CellStyle style = workbook.createCellStyle();//新建样式对象 style.setWrapText(true);//设置自动换行 XSSFFont font = (XSSFFont) workbook.createFont();//创建字体对象 font.setFontName("微软雅黑"); style.setFont(font); cell.setCellStyle(style); style.setLocked(true);//设置cell所引用的样式是否锁住 cell.setCellType(XSSFCell.CELL_TYPE_STRING);//设置单元格 格式为 字符串 cell.setCellValue("test我现在很累,你知道吗,我真的很累。我现在很累很累很累很累!~I 老虎 YOU!"); File file = new File(filePath + fileName + "." + fileType); OutputStream stream = new FileOutputStream(file); workbook.write(stream); stream.close(); } }