您的位置:首页 > 运维架构 > Apache

Java 操作 excel 文档 用 apache poi 来解决。

2016-07-09 10:49 330 查看
原文链接:https://www.geek-share.com/detail/2678993749.html

`package excel; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Calendar; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Test { private static Workbook wb = new HSSFWorkbook(); public static Font createMyFont(short point,String fontname,short color){ Font font = wb.createFont(); font.setFontHeightInPoints(point); font.setFontName(fontname); font.setColor(color); font.setItalic(true); font.setBold(true); return font;

}

public static void main(String[] args) throws Exception {
// or new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");//创建工作表
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);//设置页面打印设置, true为横版
Row row = sheet.createRow((short) 1);//构造一行, 行数为第二行
Row row1 = sheet.createRow((short) 2);//构造一行, 行数为第三行
/*
* 创建字体
*/

//  font.setStrikeout(true);

// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
CellStyle style1 = wb.createCellStyle();
style.setFont(createMyFont((short)24,"楷体",HSSFColor.LIGHT_ORANGE.index));
style1.setFont(createMyFont((short)14,"楷体",HSSFColor.RED.index));

// Create a cell and put a value in it.
Cell cell = row.createCell(1);//row变量的第二列创建一个cell对象。
cell.setCellValue("我是公司");
cell.setCellStyle(style);

Cell cell1 = row1.createCell(2);//在row1变量的第三列创建一个cell对象。
cell1.setCellValue("我是公司二");
cell1.setCellStyle(style1);

FileOutputStream fileOut = new FileOutputStream("g:\\yxdown\\workbook.xls");
wb.write(fileOut);
fileOut.close();

}

} `

转载于:https://www.geek-share.com/detail/2678993749.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐