java生成excel文件工具类实例
2014-08-11 14:20
127 查看
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.log4j.Logger;
public class ExcelUtils {
static private Logger logger = Logger.getLogger(ExcelUtils.class);
static public int SUCCESS = 0;
static public int ERROR = -1;
/**
* 创建单个sheet的excel文件,自己指定路径,用二维数组来映射相应的单元格
**/
static public int createExcel(String fileRootPath,String fileName,String sheetName,String[][] dataArr){
int result = ERROR;
try {
if(fileRootPath == null || "".equals(fileRootPath) || fileName == null || "".equals(fileName) ){
throw new Exception("创建excel文件错误:createExcel方法参数值为空!");
}
if(dataArr == null || dataArr[0] == null){
throw new Exception("输入的参数dataArr错误!");
}
String filePath = fileRootPath + fileName;
File file = new File(fileRootPath);
if(!file.exists()){
file.mkdirs();
}
WritableWorkbook book = Workbook.createWorkbook(new File(filePath));
if(sheetName == null || "".equals(sheetName)){
sheetName = "第一页";
}
// 生成名为sheetName的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(sheetName , 0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
for(int i=0 ;i<dataArr.length;i++){
for(int j=0;j<dataArr[0].length;j++){
Label label = new Label(j, i, dataArr[i][j]);
sheet.addCell(label);// 将定义好的单元格添加到工作表中
}
}
book.write();
book.close();
result = SUCCESS;
} catch (IOException e) {
logger.error("excel文件路径错误!");
logger.error(e.toString());
return result;
} catch (RowsExceededException e) {
logger.error("创建excel时,行超过!");
logger.error(e.toString());
return result;
} catch (WriteException e) {
logger.error("创建excel时,写入错误!");
logger.error(e.toString());
return result;
}catch (Exception e) {
logger.error(e.toString());
return result;
}
return result;
}
public static void main(String args[]) {
String[][] myarr = new String[2][3];
myarr[0][0]="11";
myarr[0][1]="12";
myarr[0][2]="13";
myarr[1][0]="21";
myarr[1][1]="22";
myarr[1][2]="23";
ExcelUtils.createExcel("D:/test/", "test.xls", "代理商账户列表", myarr);
}
}
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.log4j.Logger;
public class ExcelUtils {
static private Logger logger = Logger.getLogger(ExcelUtils.class);
static public int SUCCESS = 0;
static public int ERROR = -1;
/**
* 创建单个sheet的excel文件,自己指定路径,用二维数组来映射相应的单元格
**/
static public int createExcel(String fileRootPath,String fileName,String sheetName,String[][] dataArr){
int result = ERROR;
try {
if(fileRootPath == null || "".equals(fileRootPath) || fileName == null || "".equals(fileName) ){
throw new Exception("创建excel文件错误:createExcel方法参数值为空!");
}
if(dataArr == null || dataArr[0] == null){
throw new Exception("输入的参数dataArr错误!");
}
String filePath = fileRootPath + fileName;
File file = new File(fileRootPath);
if(!file.exists()){
file.mkdirs();
}
WritableWorkbook book = Workbook.createWorkbook(new File(filePath));
if(sheetName == null || "".equals(sheetName)){
sheetName = "第一页";
}
// 生成名为sheetName的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(sheetName , 0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
for(int i=0 ;i<dataArr.length;i++){
for(int j=0;j<dataArr[0].length;j++){
Label label = new Label(j, i, dataArr[i][j]);
sheet.addCell(label);// 将定义好的单元格添加到工作表中
}
}
book.write();
book.close();
result = SUCCESS;
} catch (IOException e) {
logger.error("excel文件路径错误!");
logger.error(e.toString());
return result;
} catch (RowsExceededException e) {
logger.error("创建excel时,行超过!");
logger.error(e.toString());
return result;
} catch (WriteException e) {
logger.error("创建excel时,写入错误!");
logger.error(e.toString());
return result;
}catch (Exception e) {
logger.error(e.toString());
return result;
}
return result;
}
public static void main(String args[]) {
String[][] myarr = new String[2][3];
myarr[0][0]="11";
myarr[0][1]="12";
myarr[0][2]="13";
myarr[1][0]="21";
myarr[1][1]="22";
myarr[1][2]="23";
ExcelUtils.createExcel("D:/test/", "test.xls", "代理商账户列表", myarr);
}
}
相关文章推荐
- JAVA生成EXCEL文件
- 关于动态生成WML文件的一个Java实例(1)
- java调用mysql的工具类实现数据库远程备份,还原,生成创建表的sql脚本
- java代码生成的带图片和附件的邮件发送实例
- java 中资源国际化工具类的编写实例
- JAVA生成SVG图表实例之柱状图 1.1
- Java使用JDBC连接数据库完整的工具类实例
- java生成Excel文件
- [个人总结]Java生成Excel文件
- JAVA生成EXCEL文件
- 关于动态生成WML文件的一个Java实例(2)
- java生成Excel文件(测试通过)
- 深入理解Java对象实例生成的例子!
- Java Excel API jxl入门及jxl.jar下载(java生成excel文件)
- JAVA生成SVG图表实例之柱状图 1.1
- JAVA生成EXCEL文件
- Java生成EXCEL文件
- 深入理解Java对象实例生成的例子!
- Java Excel API jxl入门(java生成excel文件)____http://solodu.javaeye.com/blog/483588
- JDK_实例(java如何生成随机数)