简体   繁体   中英

How to store CellStyle object into database?

I'am reading excel file and storing few properties like cellstyle,columnwidth,row and column index and storing in Map as follows:

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Skeleton {

    public Map<Integer, List<List<Object>>> readSkeleton(File input){

        Map<Integer, List<List<Object>>> skeletondata = new TreeMap<Integer, List<List<Object>>>();
        try {
            FileInputStream in = new FileInputStream(input);
            XSSFWorkbook wb = new XSSFWorkbook(in);

            int sheetIx = 5;   //remove if using above for loop
                XSSFSheet st = wb.getSheetAt(sheetIx); 
                int rowcount = 0;
                for (Row row:st){

                    List<List<Object>> skeletonrow = new ArrayList<List<Object>>();

                    int cellcount = 0;
                    for (Cell cell:row){

                        List<Object> skeletoncell = new ArrayList<Object>();

                        skeletoncell.add(sheetIx);   //for sheet Ix
                        skeletoncell.add(cell.getRowIndex());  //for rowIx
                        skeletoncell.add(cell.getColumnIndex());  //for columnIx

                        CellStyle cs = cell.getCellStyle();
                        int columnwidth = st.getColumnWidth(cellcount);
                        skeletoncell.add(cs);  // for cell style

                        skeletoncell.add(columnwidth);  //for column width

                        switch (cell.getCellType()) {

                        /*case Cell.CELL_TYPE_BLANK:
                            skeletoncell.add(null);
                            skeletonrow.add(skeletoncell);
                            break; 
                        case Cell.CELL_TYPE_BOOLEAN:                        
                            break;
                        case Cell.CELL_TYPE_ERROR:                                          
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            break;         */
                        case Cell.CELL_TYPE_NUMERIC:
                            skeletoncell.add(cell.toString());
                            skeletonrow.add(skeletoncell);
                            break;
                        case Cell.CELL_TYPE_STRING:
                            skeletoncell.add(cell.getStringCellValue());
                            //skeletoncell.add("Abrakadabra");
                            skeletonrow.add(skeletoncell);
                            break;
                        default:
                            skeletoncell.add(null);
                            skeletonrow.add(skeletoncell);
                            break;

                        }
                        System.out.println("skeleton cell size: "+skeletoncell.size());
                        cellcount++;
                    }
                    skeletondata.put(rowcount, skeletonrow);
                    rowcount++;
                }
                System.out.println("skeleton data :"+skeletondata);




        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return skeletondata;



    }
}

This returns a map element which contains row number as key and each cell along with its properties as value. I'am trying to store this data into database (postgres) as follows:

package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Skeleton {
    public void skeletonDataToDatabase(File input){

        DAOClass dao = new DAOClass();
        Connection con = null;
        PreparedStatement pst = null;

        con = dao.getConnection();

        try{
            Skeleton skeleton = new Skeleton();
            Map<Integer, List<List<Object>>> skeletondata = new TreeMap<Integer, List<List<Object>>>();
            skeletondata = skeleton.readSkeleton(input);
            Set<Integer> keys = skeletondata.keySet();
            for (Integer key : keys){

                List<List<Object>> skeletonrow = new ArrayList<List<Object>>();
                skeletonrow = skeletondata.get(key);

                for (int r=0;r<skeletonrow.size();r++){ 

                    List<Object> skeletoncell = new ArrayList<Object>();
                    skeletoncell = skeletonrow.get(r);

                    XSSFWorkbook wb = new XSSFWorkbook();
                    CellStyle cs1 = (CellStyle) skeletoncell.get(3);

                    //cs1.cloneStyleFrom((CellStyle) skeletoncell.get(3));  // cell style value
                    System.out.println("cwll style: "+cs1);
                       /*Schd_Id integer, 
                       SubSchd_Id integer, 
                       RowIx integer, 
                       ColIx integer, 
                       CellStyle_Value character varying(100), 
                       ColumnWidth integer,
                       Cell_Value character varying(100)*/
                    //System.out.println("fifth value: "+skeletoncell.get(5));
                    if(skeletoncell.get(5)==null){    //check for null cell value (blank)
                        //System.out.println("after if loop true ");
                        String query = "insert into Template_Skeleton(Schd_Id,SubSchd_Id,RowIx,ColIx,CellStyle_Value,ColumnWidth) " +
                                "values(?,?,?,?,?,?);";
                        pst = con.prepareStatement(query);
                        pst.setInt(1, 1);                       //Schd id
                        pst.setInt(2, (int) skeletoncell.get(0));                       //Subschd id
                        pst.setInt(3, (int) skeletoncell.get(1));  //row Ix
                        pst.setInt(4, (int) skeletoncell.get(2));  //col ix
                        pst.setObject(5, cs1);                //cellstyle value
                        pst.setInt(6, (int) skeletoncell.get(4));  //column width

                    }else{
                        System.out.println("inside else loop false");
                        String query = "insert into Template_Skeleton(Schd_Id,SubSchd_Id,RowIx,ColIx,CellStyle_Value,ColumnWidth,Cell_Value) " +
                                "values(?,?,?,?,?,?,?);";
                        //System.out.println("after query");
                        pst = con.prepareStatement(query);
                        pst.setInt(1, 1);                       //Schd id
                        pst.setInt(2, (int) skeletoncell.get(0));                       //Subschd id
                        pst.setInt(3, (int) skeletoncell.get(1));  //row Ix
                        pst.setInt(4, (int) skeletoncell.get(2));  //col ix
                        pst.setObject(5, cs1);  //cellstyle value
                        pst.setInt(6, (int) skeletoncell.get(4));  //column width
                        pst.setString(7, (String) skeletoncell.get(5));  //cell calue
                        //System.out.println("after 7th value");

                    }   
                    //System.out.println("before execute");
                    pst.executeUpdate();
                    //System.out.println("after execute");

                }
                System.out.println("inserted row :"+key);

            }

        }catch (SQLException e){
            e.printStackTrace();

        }

    }
}

While executing it shows the below error:

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.apache.poi.xssf.usermodel.XSSFCellStyle. Use setObject() with an explicit Types value to specify the type to use. at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1917) at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:36) at com.tcs.Skeleton.skeletonDataToDatabase(Skeleton.java:157) at com.tcs.Test.main(Test.java:121)

Note: main method is in test class, connection from DAOclass. I have tried to add cellstyle object as string but I want to store it as such because form database i have to render the style to create a new sheet which follows the stored style. Thanks in advance.

I would recommend serializing the cell style object and storing the serialized value. I typically use Jackson for serializing/deserializing. The cell data shouldn't be large so serializing to a String should be ok. You can the use a large varchar column or a CLOB column.

I realized, instead of storing style objects which is difficult to render, it is better to store style property values. Ex: isBold--true or false We can do this for as many properties as we need and store as such in database with same style property column name. While rendering we can use the same values while setting the property value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM