Java 操作 excel 文档 用 apache poi 来解决。
2016-07-09 00:00
573 查看
`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;
}
`
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(); }
}
`
相关文章推荐
- Ubuntu下Apache建立虚拟主机
- IE 11 请求语言zh-Hans-CN所引发的问题及解决方案
- Apache服务器运维笔记(2)----使用apxs来进行编译安装 mod_txt 模块
- wordpress-4.5.3+apache24+mysql5.6+php5.6+windows7x64相关配置
- ubuntu16.04配置Lamp环境(搭建linux+apache+mysql+php7环境
- Apache服务器运维笔记(2)----使用<IfDefine>容器实现不同的配置
- Apache服务器运维笔记(1)----运行多个Apache服务器
- Apache Geode Region 压缩介绍
- PHP编程之设置apache虚拟目录
- 如果使用mac自带的apache2 如何运行php文件
- apache commons fileupload 依赖(2016-07-01更新)
- org.apache.axis2.AxisFault: First Element must contain the local name, Envel
- Apache Shiro 整合Spring 进行权限验证 以及在Freemarker中使用shiro标签
- 安装xampp但是apache无法启动
- apache shiro框架的@RequiresRoles 不起作用
- spark编译问题解决 object apache is not a member of package org
- PHP编程之设置apache虚拟目录
- 使用 Apache Solr 实现更加灵巧的搜索,第 1 部分: 基本特性和 Solr 模式
- 使用 Apache Solr 实现更加灵巧的搜索,第 2 部分: 用于企业的 Solr
- apache 优化