java excel 删除单元格格式_Java 设置单元格 格式 Excel

package *.*;

import java.util.ArrayList;

import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class FontCellStyle {

private static HSSFFont fontStyle = null;

private static HSSFCellStyle cellStyle = null;

public static HSSFFont getHdrFont(HSSFWorkbook wb) {

fontStyle =

wb.createFont();

fontStyle.setFontName("宋体");

fontStyle.setFontHeightInPoints((short)20);

fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

return fontStyle;

}

public static HSSFFont getFtrFont(HSSFWorkbook wb) {

fontStyle =

wb.createFont();

fontStyle.setFontName("宋体");

fontStyle.setFontHeightInPoints((short)12);

fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

return fontStyle;

}

public static HSSFFont getContentFont(HSSFWorkbook wb) {

fontStyle =

wb.createFont();

fontStyle.setFontName("宋体");

fontStyle.setFontHeightInPoints((short)12);

fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

return fontStyle;

}

public static HSSFFont getMergeConflictFont(HSSFWorkbook wb)

{

fontStyle =

wb.createFont();

fontStyle.setFontName("Arial");

fontStyle.setFontHeightInPoints((short)12);

fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

return fontStyle;

}

public static HSSFCellStyle getAnyCellStyle(HSSFWorkbook

wb,HSSFFont font,short align,short valign,short indent,boolean

wrapText) {

cellStyle

=wb.createCellStyle();

if(font != null)

cellStyle.setFont(font);

if(align > 0)

cellStyle.setAlignment(align);

if(valign > 0)

cellStyle.setVerticalAlignment(valign);

if(indent > 0)

cellStyle.setIndention(indent);

cellStyle.setWrapText(wrapText);

return cellStyle;

}

public static void setDefaultHighWidth(HSSFSheet sheet) {

sheet.setDefaultRowHeightInPoints(10);

sheet.setDefaultColumnWidth((short) 20);

}

public static void setDefaultCellHighWidthInRange(HSSFSheet

sheet,short[] eachCellWidth,int high) {

//假定第一行和第一行所需的单元个已经建立好了,也就是说,在这之前已经调用了DesignXlsHeaderFooter.setXlsHeader

sheet.setDefaultRowHeightInPoints(high);//设置默认高

for(int i = 0;i

< eachCellWidth.length;i++) {

//System.out.print(""+i+"\t");

sheet.setColumnWidth((short) i,(short)

((eachCellWidth[i])*256));

}

//System.out.println();

}//end_setDefaultCellHighWidthInRange

}

FileOutputStream fos = null;

try {

fos = new

FileOutputStream(rptRealPathAndName);

} catch (FileNotFoundException

e) {

// TODO

Auto-generated catch block

//System.out.println("创建文件失败。。。"); log.info("In

WriteRptByType.writeRptTypeFive(),create file failed!!!");

log.error(e.getMessage());

//e.printStackTrace();

return

-1;

}

HSSFWorkbook workBook = new HSSFWorkbook();

HSSFSheet sheet = workBook.createSheet();;

workBook.setSheetName(0,"移动",HSSFWorkbook.ENCODING_UTF_16);

HSSFCellStyle cellStyleHdr =

FontCellStyle.getAnyCellStyle(workBook,FontCellStyle.getHdrFont(workBook),HSSFCellStyle.ALIGN_CENTER,

HSSFCellStyle.VERTICAL_CENTER, (short)-1, true);

HSSFRow curRow = sheet.createRow(0);

HSSFCell curCell= curRow.createCell((short)0);

curCell.setEncoding(HSSFCell.ENCODING_UTF_16);

curCell.setCellStyle(cellStyleHdr );

curCell.setCellValue("可以写入汉字,无乱码");

try {

workBook.write(fos);

fos.close();

} catch (IOException e)

{

// TODO

Auto-generated catch block

//System.out.println("写错误。。。");

succFlag =

-1;

log.error("报表写错误:"+e.getMessage());

//e.printStackTrace();

你可能感兴趣的:(java,excel,删除单元格格式)