日期:2014-05-20  浏览次数:20908 次

有谁知道excel报表中的样式如何复制呢?
我现在要做一个报表,
第一页和第二页都是定好的,具有很多样式。。
我本地数据库有商品数据。
我需要把数据写到第三页当中,有的甚至需要写到第四页当中。。。
写入几页数据是由我查询出来的商品名决定的
但是我貌似每次写的都是都是写入到了第一页当中,而且我样式都没有了。。。
而且每张报表的数据都不同,我需要根据不同的报表写入不同的数据,,

求助大神。。。。

------解决方案--------------------
poi的

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCellStyle style = workbook.getCellStyleAt((short)10);


查一下api,网上搜一下,应该很快能找到你想要的
------解决方案--------------------
楼主的思路就有问题,,
如果你第一页,第二页不打算修改的,
从第三页开始修改,那么你直接获取这张表对象,然后从第三页开始写就OK了。。。
至于表名,你可以把一个样本excel表复制N多次,然后先用一个程序修改名称。

然后第二个程序开始读excel,根据excel名的不同写入不同数据就可以了。。

还是提供一个工具类吧,可以写入一个excel表格下的任何一张表。。

package cn.magaseek.mj.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/*
 * Date:2013年1月30日11:39:36
 * Author:leilei
 */
//该类是一个工具类,实现的功能是对execl文件简单读和存的功能
public class ExcelHelper {
// 该方法实现的功能是读,读的时候全部以String的方式存储,如果小数点后面为0的全部忽略,
public static String[][] poiReader(String filepath, File file) {
String[][] result = null;
InputStream is = null;
try {
if (filepath != null && !filepath.equals("")) {
// 输入输出流
is = new FileInputStream(filepath);
} else if (file.exists()) {
is = new FileInputStream(file);

} else {
System.out.println("输入的路径和文件为空");
}

// 创建工作空间
Workbook wb = null;

wb = WorkbookFactory.create(is);

// 获取工作表
Sheet sheet = wb.getSheetAt(0);// 获取第一个工作表
// 工作行
Row row;
// 工作单元格
Cell cell = null;
int rownum;// 行
int columnnum;// 列
rownum = sheet.getLastRowNum() + 1;
columnnum = sheet.getRow(0).getLastCellNum();
// 实例化返回的数组对象
result = new String[rownum][columnnum];

System.out.println("rownum:" + rownum);
System.out.println("columnnum:" + columnnum);

for (int i = 0; i < rownum; i++) {
row = sheet.getRow(i);
// 该行为空的话跳过,不加入数组
if (row == null)
continue;
for (int j = 0; j < columnnum; j++) {
cell = row.getCell(j);
// 该单元格为空的话,设为空
if (cell == null) {
result[i][j] = "";
} else {
String str = cell.toString();
if (str.contains(".")
&& str.matches("[0-9]{1,}.[0-9]{1,}")) {
String[] s = str.split("\\.");
if (Integer.parseInt(s[1]) == 0) {
result[i][j] = s[0];
} else {
result[i][j] = str;
}
} else {
result[i][j] = str;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 该方法实现的功能是读,读的时候全部以String的方式存储,如果小数点后面为0的全部忽略,
public static String[][] poiReader(String filepath, File file, int k) {
String[][] result;
InputStream is = null;
try {
if (filepath != null && !filepath.equals("")) {
// 输入输出流
is = new FileInputStream(filepath);
} else if (file.exists()) {
is = new FileInputStream(file);

} else {
System.out.println("输入的路径并且文件为空");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 创建工作空间
Workbook wb = null;
try {
wb = WorkbookFactory.create(is);
} catch (Exception e) {
e.printStackTrace();
}
// 获取工作表
Sheet sheet = wb.getSheetAt(k);// 获取第一个工作表
// 工作行
Row row;
// 工作单元格
Cell cell = null;