apache POI学习(三)——生成带格式的excel表格
2016-08-31 18:01
447 查看
apache POI学习(三)——生成带格式的excel表格
根据官方提供的代码,写一个带有简单格式的excel。import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import javax.lang.model.element.VariableElement; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub // create a new file FileOutputStream out = null; try { out = new FileOutputStream("d:/123/workbook.xls"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // create a new workbook Workbook wb = new HSSFWorkbook(); // create a new sheet Sheet s = wb.createSheet(); // set the SheetName wb.setSheetName(0, "sheet name00"); //item CellStyle CellStyle cs = wb.createCellStyle(); Font f = wb.createFont(); f.setFontHeightInPoints((short)Font.COLOR_RED); f.setColor((short)0xc); cs.setFont(f); //title CellStyle CellStyle csTitle = wb.createCellStyle(); Font fTitle = wb.createFont(); fTitle.setFontHeightInPoints((short)30); fTitle.setColor((short)Font.COLOR_RED); fTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); csTitle.setFont(fTitle); // declare a row object reference Row r = null; // declare a cell object reference Cell c = null; s.setDefaultColumnWidth(25); r = s.createRow(0); r.setHeightInPoints((short)40); c = r.createCell(1); c.setCellValue("this is a test title!"); c.setCellStyle(csTitle); r=s.createRow(1); c= r.createCell(2); c.setCellValue("打印时间:2016-8-31 17:47:18"); for (int rownum = 2; rownum < 6; rownum++) { r = s.createRow(rownum); for (int cellnum = 0; cellnum < 4; cellnum++) { c = r.createCell(cellnum); c.setCellValue("rownum=" + rownum + " colnum=" + cellnum); c.setCellStyle(cs); } } // demonstrate adding/naming and deleting a sheet // create a sheet, set its title then delete it /*s = wb.createSheet(); wb.setSheetName(1, "DeletedSheet"); wb.removeSheetAt(1);*/ //end deleted sheet // write the workbook to the output stream // close our file (don't blow out our file handles try { wb.write(out); out.close(); wb.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
生成的excel截图:
相关文章推荐
- 关于poi导出大数据量EXCEL导出缓慢的解决方案--生成xml格式的表格文件(注意xls格式为03 07下格式会不一样)
- c#读取生成excel表格文件xls、xlsx格式文件
- web利用table表格生成excel格式问题
- Html Table用JS导出excel格式问题 导出EXCEL后单元格里的000412341234会变成412341234 7-14 会变成 2018-7-14(7月14) 自定义格式 web利用table表格生成excel格式问题 js导出excel增加表头、mso-number-format定义数据格式 数字输出格式转换 mso-number-format:"\@"
- 【学习笔记二】struts2的动态下载,使用Apache PoI生成excel表格,将用户信息以xls文件格式供用户下载
- C# 生成Excel文件及表格内容格式编辑 excel模板流
- 简单OA项目笔记(5):生成、下载excel格式的动态表格(POI)
- 使用Apache POI生成指定格式的Excel文件
- 动态生成DataSet并填充值;并将DataSet导成Excel格式
- OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码)
- JSP中将数据生成Excel格式 POI
- excel表格中,公式生成的数据如何复制到另一个工作簿中?
- excel 在同一张工作表中复制表格,格式会改变的解决
- 把数据库查询信息直接生成网页嵌入EXCEL表的格式,并可导出成EXCEL文件
- C#中设置EXCEL文件中表格边框格式
- .net导出EXCEL文件操作类包括格式较复杂表格导出
- 如何在后台运行模式生成Excel格式文件(收藏)
- 将DataGrid内容生成标准的Excel格式文件
- php根据xml生成带格式的EXCEL
- Excel学习(五)生成12位以上的规则编号