POI设置Excel的格式字体样式美化POI设置Excel的格式字体样式美化
import java.io.FileOutputStream;
import org.apache.poi.hssf.urmodel.HSSFCell;
import org.apache.poi.hssf.urmodel.HSSFCellStyle;
map worldimport org.apache.poi.hssf.urmodel.HSSFFont;
import org.apache.poi.hssf.urmodel.HSSFRichTextString;
import org.apache.poi.hssf.urmodel.HSSFRow;
import org.apache.poi.hssf.urmodel.HSSFSheet;no other
import org.apache.poi.hssf.urmodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
/**
*
* @author hadeslee
*/
public class Test2{
/** Creates a new instance of Test2 */
public Test2() {
}
public static void main(String[] args)throws Exception {
//声明⼀个⼯作薄
HSSFWorkbook wb=new HSSFWorkbook();
//⽣成⼀个表格
HSSFSheet ateSheet("表格1");
//⽣成⼀个列
HSSFRow ateRow(0);
//⽣成⼀个样式
HSSFCellStyle ateCellStyle();
rewarded//设置这些样式
skto
style.tFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.tFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.tBorderBottom(HSSFCellStyle.BORDER_THIN);
style.tBorderLeft(HSSFCellStyle.BORDER_THIN);
style.tBorderRight(HSSFCellStyle.BORDER_THIN);
style.tBorderTop(HSSFCellStyle.BORDER_THIN);
style.tAlignment(HSSFCellStyle.ALIGN_CENTER);
//⽣成⼀个字体
就教的意思
HSSFFont ateFont();
font.tColor(HSSFColor.VIOLET.index);
font.tFontHeightInPoints((short)16);
font.tBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//把字体应⽤到当前的样式
style.tFont(font);
//填充单元格
for(short i=0;i<5;i++){
/
/声明⼀个单元格
HSSFCell ateCell(i);
//设置单元格的字符值
cell.tCellValue(new HSSFRichTextString("第"+i+"列"));
//设置单元格的样式
cell.tCellStyle(style);cdec
}
FileOutputStream fout=new FileOutputStream("我的第⼀个EXCEL.xls");
//输出到⽂件
wb.write(fout);
fout.clo();
}
}
public static void main(String[] args) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = ateRow(0);
row.tHeight((short) 25);//⽬的是想把⾏⾼设置成25px
FileOutputStream fileOut = new FileOutputStream("c:\\a.xls");
wb.write(fileOut);
fileOut.clo();
}
catch (Exception e) {
e.printStackTrace();
}
}
打开a.xls发现结果不是我想要的,第⼀⾏的⾼度都没有,没有报错说明代码有问题,为什么回没有⾼
度呢?是不是单位不⼀样呢?我把row.tHeight((short) 25);改成了row.tHeight((short) 250);结果发现第⼀⾏出来了,但是这是怎么⼀个换算关系呢?我查看了⼀下导出的Excel第⼀⾏⾼是16像素,换算⼀下得出row.tHeight((short) 15.625);表⽰⾏⾼为⼀个像素,那么想设成⼏个像素就好做了。⽐如
row.tHeight((short) (15.625*n));//n为⾏⾼的像素数。
其实在API中还有⼀个HSSFRow 对象还有⼀个设置⾏⾼的函数tHeightInPoints(float height);这个函数中参数就是⾏⾼的像素数,⽐tHeight函数要⽅便多了。
⾏⾼设置完成了,接下来设置列宽
public static void main(String[] args) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = ateRow(0);
row.tHeight((short) 250);
sheet.tColumnWidth((short) 0, (short) 250);
FileOutputStream fileOut = new FileOutputStream("c:\\a.xls");
wb.write(fileOut);
fileOut.clo();
}
catch (Exception e) {
e.printStackTrace();
}
}
接下来说说sheet.tColumnWidth((short) 0, (short) 250);
第⼀个参数表⽰要为第⼏列设置,第⼆个参数表⽰列的宽度,看看上⾯的代码按说第⼀⾏第⼀列的单元格形状应该是个正⽅形,因为宽和⾼都是250,但是打开导出后的Excel发现宽度没有⾼度⼤,是个长⽅形,查看该列的宽度仅为7个像素,看来⾏⾼和列宽的单位是不⼀样的,同样换⼀算sheet.tColumnWidth((short) 0, (short) (35.7));表⽰⾼度为⼀个像素,同样设置列宽的像素为sheet.tColumnWidth((short) 0, (short) (35.7*n));//n为列⾼的像素数。
public class MergedCells {various是什么意思
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
台风茉莉HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = ateRow((short) 1);
HSSFCell cell = ateCell((short) 1);
cell.tCellValue("This is a test of merging");
sheet.addMergedRegion(new Region(1, (short) 1, 1, (short) 2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut);
fileOut.clo();
}
}
public class WorkingWithFonts {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
discover// Create a row and put some cells in it. Rows are 0 bad.
HSSFRow row = ateRow((short) 1);
// Create a new font and alter it.
cet4真题
HSSFFont font = wb.createFont();
font.tFontHeightInPoints((short) 24);
font.tFontName("Courier New");
font.tItalic(true);
font.tStrikeout(true);
// Fonts are t into a style so create a new one to u.
HSSFCellStyle style = wb.createCellStyle();
style.tFont(font);
// Create a cell and put a value in it.
HSSFCell cell = ateCell((short) 1);
cell.tCellValue("This is a test of fonts");
cell.tCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut);
fileOut.clo();
}
}
HSSFRow row = ateRow(2);
row.tHeightInPoints(240);
sheet2.tColumnWidth((short) 2, (short) 9000);