package com.jim.tools;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 此类采用 poi包 实现了向 .xls 文件写入的功能,
* 目前为止 FastExcel 还没有提供向 .xls 文件写入的功能
* @author hellojim
* @company cxtech
*/
public class POIWriteToExcel {
private String URL; //要写入的 .xls 文件的目录
private String index; //要写入的 .xls 文件的那个 Sheet 的名字, 默认为第0个(Sheet1)
private int beginRow = 0; //要写入的 Sheet 的开始行, 默认为第0行
private int beginCol = 0; //要写入的 Sheet 的开始列, 默认为第0列
private String[][] content; //要写入到 Sheet 中的内容
/**
* URL 要写入的那个 .xls 文件的地址
* content 要写入到 Sheet 中的内容
*/
public POIWriteToExcel(String URL,String[][] content) {
//这里简单的做了个判断
if(URL == null || URL.trim().equals("")) {
System.out.println("文件不能为空!");
}else if(URL.trim().indexOf(".xls") == -1 && URL.trim().indexOf(".XLS") == -1) {
System.out.println("文件格式不正确!");
}else {
this.URL = URL;
this.content = content;
}
}
/**
* URL 要写入的那个 .xls 文件的地址
* content 要写入到 Sheet 中的内容
* index 要写入的那个 Sheet , 默认为第一个(Sheet1)
*/
public POIWriteToExcel(String URL,String[][] content, String index) {
this(URL,content);
this.index = index;
}
/**
* URL 要写入的那个 .xls 文件的地址
* content 要写入到 Sheet 中的内容
* index 要写入的那个 Sheet , 默认为第一个(Sheet1)
* beginRow 要写入的 Sheet 的开始行
* beginCol 要写入的 Sheet 的开始列
*/
public POIWriteToExcel(String URL,String[][] content,String index,int beginRow,int beginCol) {
this(URL,content,index);
this.beginRow = beginRow;
this.beginCol = beginCol;
}
public int getBeginCol() {
return beginCol;
}
public void setBeginCol(int beginCol) {
this.beginCol = beginCol;
}
public int getBeginRow() {
return beginRow;
}
public void setBeginRow(int beginRow) {
this.beginRow = beginRow;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getURL() {
return URL;
}
public void setURL(String url) {
URL = url;
}
public String[][] getContent() {
return content;
}
public void setContent(String[][] content) {
this.content = content;
}
public void write() throws FileNotFoundException, IOException {
//创建新的 Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//在 Excel 工作簿中建创一个工作表,其名为缺省值 sheet1
HSSFSheet sheet = workbook.createSheet(String.valueOf(index));
//创建字体
HSSFFont font = workbook.createFont();
//把字体颜色设置为红色
font.setColor(HSSFFont.COLOR_NORMAL);
//把字体设置为粗体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//创建格式
HSSFCellStyle cellStyle= workbook.createCellStyle();
//把创建的字体付加于格式
cellStyle.setFont(font);
int beginRow = this.beginRow;
int beginCol = this.beginCol;
for(int i=0; i<content.length; i++) {
//在工作表中创建一行
HSSFRow row = sheet.createRow(beginRow++);
beginCol = this.beginCol;
for(int j=0; j<content[i].length; j++) {
//在一行中创建一个表格
HSSFCell cell = row.createCell((short)beginCol++);
if( i == 0) {
//把上面的格式付加于一个单元格
cell.setCellStyle(cellStyle);
}
//设置此单元格中存入的是字符串
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//设置编码 这个是用来处理中文问题的
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//向此单元格中放入值
cell.setCellValue(content[i][j]);
}
}
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(this.URL);
workbook.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
public static void main(String [] args) {
String[][] content = new String[2][3];
content[0][0] = "1";
content[0][1] = null;
content[0][2] = "3";
content[1][0] = "4";
content[1][1] = "5";
content[1][2] = "6";
POIWriteToExcel poiwte = new POIWriteToExcel("d:\\abc.xls",content,"sheet5",5,5);
// poiwte.setIndex("sheet3");
// poiwte.setBeginRow(2);
// poiwte.setBeginCol(3);
try {
poiwte.write();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
好了,现在在 d:\ 目录下会有一个 abc.xls 文件
分享到:
相关推荐
POI读写excel(.xls/.xlsx)的Demo,直接import到myeclipse即可运行。 IDE:myeclipse 9.1 含jar包
poi颜色对照表.xls
org.apache.poi JAR包,解决个人的 import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi...
提供POI编译DEX前的jar包。 提供POI编译DEX前的jar包。 提供POI编译DEX前的jar包。用于Android解析Excel的工程查看
org.apache.poi JAR包,解决import org.apache.poi.hssf.usermodel.HSSFWorkbook; 支持office全系excel文件解析。 import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; ...
使用poi生成Excel所需要的jar包,poi-3.9.jar
使用poi解析excel文件,并将数据写入到数据库 项目说明 这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL...
使用poi将xls格式的Excel转为xlsx,文件中的所有样式都可以完美转换,例如合并栏、背景和单元格样式等。Java源码
POI处理excel文件,主要包括Excel的读取、写入,各种数据格式处理、单元格合并、注释、下拉列表及单元格的边框、背景色、宽度调整等。
org.apache.poi.xwpf.converter.pdf-1.0.6.jar 文件 org.apache.poi.xwpf.converter.pdf-1.0.6 org.apache.poi.xwpf.converter.core-1.0.6 org.apache.poi.xwpf.converter-0.9.1
使用JDK1.4,poi读取.doc的内容和表格内容以及.xls,不支持docx和xlsx。
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
注:下文中的 *** 代表文件名中的版本号。 # 【poi-***.jar中文文档.zip】 中包含: 中文文档:【poi-***-javadoc-...org.apache.poi.EncryptedDocumentException org.apache.poi.OldFileFormatException ...... ```
org.apache.poi.xwpf.converter.pdf-1.0.6 org.apache.poi.xwpf.converter.core-1.0.6 org.apache.poi.xwpf.converter-0.9.1
fr.opensagres.poi.xwpf.converter.core-2.0.2、fr.opensagres.poi.xwpf.converter.pdf-2.0.1
org.apache.poi JAR包,解决个人的 import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi...
1.java使用poi生成excel插入图表曲线 2.demo包含完成jar包 3.主要用途是过滤数据自动生成曲线图
Java用POI实现根据Excel表格模板生成新的Excel并实现数据输出...
org.apache.poi.xwpf.converter.core-1.0.6org.apache.poi.xwpf.converter.core-1.0.6org.apache.poi.xwpf.converter.core-1.0.6org.apache.poi.xwpf.converter.core-1.0.6
poi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excel