import java.io.FileOutputStream;
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.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class Excel {
public static void main(String argv[]) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
//第一个样式和输出(边框)
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底线和颜色
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边线和颜色
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边线和颜色
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置上面线和颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("hello"));
cell.setCellStyle(style);
row = sheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("hello"));
//第二个样式和输出(背景/前景色)
style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.ORANGE.index); //添加背景色,内容看不清楚
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
row = sheet.createRow(2);
cell = row.createCell(2);
cell.setCellValue(new HSSFRichTextString("hello"));
cell.setCellStyle(style);
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index); //添加前景色,内容看的清楚
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
row = sheet.createRow(3);
cell = row.createCell(3);
cell.setCellValue(new HSSFRichTextString("hello"));
cell.setCellStyle(style);
//第三个样式和输出(字体)
HSSFFont font = wb.createFont(); //设置字体的样式
font.setFontHeightInPoints((short) 8); //字体大小
font.setFontName("Arial"); //什么字体
font.setItalic(false); //是不倾斜
font.setStrikeout(false); //是不是划掉
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体加粗
style = wb.createCellStyle(); //引用字体的这个样式
style.setFont(font);
row = sheet.createRow(4); //输出看结果
cell = row.createCell(4);
cell.setCellValue(new HSSFRichTextString("BJ10012-1241"));
// cell.setCellStyle(style);
//第四样式和输出(自定义颜色)
style = wb.createCellStyle(); //设置颜色样式,后面定义颜色的值
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
row = sheet.createRow(5);
cell = row.createCell(5);
cell.setCellValue(new HSSFRichTextString("BJ10012-1241"));
// cell.setCellStyle(style);
HSSFPalette palette = wb.getCustomPalette(); //定义颜色的值
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 0,
(byte) 0);
//第五样式和输出(水平和垂直对齐)
style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
row = sheet.createRow(6);
cell = row.createCell(6);
cell.setCellValue(new HSSFRichTextString("BJ10012-1241"));
cell.setCellStyle(style);
//设置一个标题样式
style = wb.createCellStyle();
font = wb.createFont(); //设置字体的样式
font.setFontHeightInPoints((short) 8); //字体大小
font.setFontName("Arial"); //什么字体
font.setItalic(false); //是不倾斜
font.setStrikeout(false); //是不是划掉
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体加粗
style = wb.createCellStyle(); //引用字体的这个样式
style.setFont(font);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底线和颜色
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边线和颜色
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边线和颜色
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置上面线和颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
style.setFillForegroundColor(HSSFColor.ORANGE.index); //添加前景色,内容看的清楚
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
row = sheet.createRow(7);
cell = row.createCell(7);
cell.setCellValue(new HSSFRichTextString("BJ10012-1241"));
cell.setCellStyle(style);
//输出文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
}
}