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

解决方案 »

  1.   

    poi的HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.getSheetAt(0);
    HSSFCellStyle style = workbook.getCellStyleAt((short)10);
    查一下api,网上搜一下,应该很快能找到你想要的
      

  2.   

    楼主的思路就有问题,,
    如果你第一页,第二页不打算修改的,
    从第三页开始修改,那么你直接获取这张表对象,然后从第三页开始写就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;
    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);
    for (int j = 0; j < columnnum; j++) {
    cell = row.getCell(j);
    if (cell == null) {
    String str = "";
    result[i][j] = str;
    } 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;
    }
    }
    }
    }
    return result;
    } // 输入二维数组和文件存放的地址,该方法把数组中的内容(包括表头)存入execl文件,返回文件存放地址
    // 注:方法本身没有判断文件是否属于execl文件的功能,存的时候不附带任何格式。
    public static String poiWriteWithoutFile(String[][] str, String filepath) {
    try {
    if (str == null) {
    return "";
    }
    HSSFWorkbook wb = new HSSFWorkbook();
    int rownum = str.length;
    int columnum = str[0].length;
    Sheet sheet = wb.createSheet();
    for (int i = 0; i < rownum; i++) {
    // System.out.println("i:"+i);
    Row row = sheet.createRow(i);
    for (int j = 0; j < columnum; j++) {
    /* System.out.println("j:"+j); */
    Cell cell = row.createCell(j);
    // 设置格式
    cell.setCellType(Cell.CELL_TYPE_STRING);
    // 设置值
    str[i][j] = (str[i][j] == null) ? "" : str[i][j];
    cell.setCellValue(str[i][j]);
    }
    }
    FileOutputStream out = new FileOutputStream(filepath);
    wb.write(out);
    } catch (IOException e) {
    e.printStackTrace();
    }
    return filepath;
    } // 注:方法本身没有判断文件是否属于execl文件的功能,存的时候不附带任何格式。
    public static String poiWrite(String[][] str, String filepath) {
    InputStream inp;
    try {
    inp = new FileInputStream(filepath);
    int rownum = str.length;
    int columnum = str[0].length; Workbook wb = WorkbookFactory.create(inp);
    Sheet sheet = wb.getSheetAt(0);
    for (int i = 0; i < rownum; i++) {
    // System.out.println("i:"+i);
    Row row = sheet.createRow(i);
    for (int j = 0; j < columnum; j++) {
    /* System.out.println("j:"+j); */
    Cell cell = row.createCell(j);
    // 设置格式
    cell.setCellType(Cell.CELL_TYPE_STRING);
    // 设置值
    if (str[i][j] != null) {
    cell.setCellValue(str[i][j]);
    } else {
    cell.setCellValue("");
    }
    }
    }
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream(filepath);
    wb.write(fileOut);
    fileOut.close();
    inp.close();
    System.out.println("写入完成,路径为" + filepath);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return filepath;
    }
    // 注:方法本身没有判断文件是否属于execl文件的功能,存的时候不附带任何格式。
    public static String poiWrite(String[][] str, String filepath,int k) {
    InputStream inp;
    try {
    inp = new FileInputStream(filepath);
    int rownum = str.length;
    int columnum = str[0].length; Workbook wb = WorkbookFactory.create(inp);
    Sheet sheet = wb.getSheetAt(k);
    for (int i = 0; i < rownum; i++) {
    // System.out.println("i:"+i);
    Row row = sheet.createRow(i);
    for (int j = 0; j < columnum; j++) {
    /* System.out.println("j:"+j); */
    Cell cell = row.createCell(j);
    // 设置格式
    cell.setCellType(Cell.CELL_TYPE_STRING);
    // 设置值
    if(str[i][j]!=null){
    cell.setCellValue(str[i][j]);
    }else{
    cell.setCellValue("");
    }
    }
    }
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream(filepath);
    wb.write(fileOut);
    fileOut.close();
    inp.close();
    System.out.println("写入完成,路径为" + filepath);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return filepath;
    }


    public static void poiWriteOut(String[][] str,OutputStream out){
    if (str == null) {
    return;
    }
    HSSFWorkbook wb=new HSSFWorkbook();
    int rownum = str.length;
    int columnum = str[0].length;
    Sheet sheet = wb.createSheet();
    for (int i = 0; i < rownum; i++) {
    // System.out.println("i:"+i);
    Row row = sheet.createRow(i);
    for (int j = 0; j < columnum; j++) {
    /* System.out.println("j:"+j); */
    Cell cell = row.createCell(j);
    // 设置格式
    cell.setCellType(Cell.CELL_TYPE_STRING);
    // 设置值
    str[i][j]=(str[i][j]==null)?"":str[i][j];
    cell.setCellValue(str[i][j]);
    }
    }
    try {
    wb.write(out);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    // 读取execl转换成map
    public static Map<String, String> execlToMap(String path,File file,int keyline,int valueline) {
    Map<String, String> map = new HashMap<String, String>();
    String[][] s;
    if(file==null){
    s = ExcelHelper.poiReader(path, null);
    }else{
    s= ExcelHelper.poiReader("", file);
    }
    for (int i = 0; i < s.length; i++) {
    for (int j = 0; j < s[0].length; j++) {
    if (map.get(s[i][keyline]) == null) {
    map.put(s[i][keyline], s[i][valueline]);
    } else {
    String key = s[i][keyline];
    String value = map.get(key);
    if (value.equals(s[i][valueline])) {
    continue;
    } else {
    map.put(key, value + "," + s[i][valueline]);
    }
    }
    }
    }
    return map;
    }
    public static void mapToExecl(String path,Map<String,String> map){
    String[][] str=new String[map.size()][2];
    int i=0;
    for(String key:map.keySet()){
    str[i][0]=key;
    str[i++][1]=map.get(key);
    }
    poiWrite(str, path);
    }
    }