是否可以将resultSet存储到多维数组中,并使用同一数组将值导出到Excel工作表

时间:2019-01-14 21:42:38

标签: java ms-access jdbc apache-poi ucanaccess

我已将结果集导出到excel表格,最后导出的excel已导出。

import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.BasicConfigurator;
import org.apache.poi.hpsf.Array;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DF3 {

    public static void main(String[] args) {

        String dayno;

        /// variables
        BasicConfigurator.configure();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet spreadsheet = workbook.createSheet("UserIDs");
        //XSSFRow headerRow = spreadsheet.createRow(0);

        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        }
        catch(ClassNotFoundException cnfex) {
            System.out.println("Problem in loading" + " MS Access JDBC driver");
            cnfex.printStackTrace();
        }

        try {
            String AccessDBName = "C:\\Users\\cderf\\Desktop\\assignment\\cderf.accdb";
            String dbURL = "jdbc:ucanaccess://"+ AccessDBName;
            connection = DriverManager.getConnection(dbURL);
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT ID_CLERK, NAM_FIRST, NAM_LAST, LAST_LOGIN ORGID FROM cderf where cde_status = 'A' and nam_role = 'Security Admin'");

            XSSFRow row = spreadsheet.createRow(0);
            XSSFCell cell;
            cell = row.createCell(0);
            cell.setCellValue("ID_CLERK");
            cell = row.createCell(1);
            cell.setCellValue("NAM_FIRST");
            cell = row.createCell(2);
            cell.setCellValue("NAM_LAST");
            cell = row.createCell(3);
            cell.setCellValue("LAST_LOGIN");
            int i = 1;

            while(resultSet.next()) {

                row = spreadsheet.createRow(i);
                cell = row.createCell(0);
                cell.setCellValue(resultSet.getString("ID_CLERK"));
                cell = row.createCell(1);
                cell.setCellValue(resultSet.getString("NAM_FIRST"));
                cell = row.createCell(2);
                cell.setCellValue(resultSet.getString("NAM_LAST"));

                //cell = row.createCell(3);
                //cell.setCellValue(resultSet.getString("LAST_LOGIN"));

                cell = row.createCell(3);
                dayno=  resultSet.getString("LAST_LOGIN");
                if(dayno.length()<8) {
                    String day = dayno.substring(1, 5).trim();
                    int date = Integer.parseInt(day) +1;
                    int retrodate = getDate(date);
                    cell.setCellValue(retrodate);
                }
                else {
                    cell.setCellValue(resultSet.getString("LAST_LOGIN"));
                }
                i++;


            }
            /* Sheet sheet = workbook.getSheetAt(0);
            Cell cell2Update = sheet.getRow(1).getCell(3);
            cell2Update.setCellValue("=TODAY()-189");*/

            String outputDirPath = "C:\\Users\\dddd\\Desktop\\eclipse\\Workspacevl\\DBTest\\ExportedExcels\\UID_SHEET.xlsx";
            FileOutputStream fileOut = new FileOutputStream(outputDirPath);
            workbook.write(fileOut);
            fileOut.close();
            resultSet.close();
            connection.close();
        }
        catch(SQLException sqlex){
            sqlex.printStackTrace();
        }
        catch(IOException e) {
        }
    }

    private static String[] resultSet() {
        // TODO Auto-generated method stub
        return null;
    }

    public static int getDate(int date){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -date); //minus number would decrement the days
        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");

        return (Integer.parseInt(sdf.format(cal.getTime()).toString()));
    }
}

但是我想更多地修改代码,以便我可以基于Last_login日期导出更精确的结果,例如,用户在前端页面上插入两个日期范围bw 20180709和20181231,导出的工作表应仅包含与last_login日期匹配的数据。是否可以使用数组,所以我可以使用if Condition声明日期来导出结果集。

我不知道数组是否适合编写代码,基本上是在学习,所以请帮忙。

打印的excel: 我

ID_CLERK  NAM_FIRST  NAM_LAST   LAST_LOGIN
--------  ---------  --------   ----------
BHEI00    ddadf      ddd        20181009
CMCRdT2   dvvcf      ffffad     20180708  
FFLN0     ass        ghiuhkkj   20180827  
Hfhudd2   HddaD      MdffrTT    20181105  
Ldfss0    labA       ronSON     20181105  
MadfLA    ngams      anddas     20181021  
TdsfS1    TED        bark       20181105

1 个答案:

答案 0 :(得分:0)

根据对问题的评论中的建议,您可以提示用户输入开始日期和结束日期,并在提交查询时仍使用这些值。这样,您无需检索所有可能的结果,然后对开始/结束日期进行过滤,而只需检索实际需要的行即可。

您可以通过使用参数化查询来实现,例如

// values from user, parsed and converted to java.sql.Date
//   startDate
//   endDate

PreparedStatement ps = connection.prepareStatement(
          "SELECT ID_CLERK, NAM_FIRST, NAM_LAST, LAST_LOGIN "
        + "FROM cderf "
        + "WHERE cde_status=? AND nam_role=? AND LAST_LOGIN BETWEEN ? AND ? ");
ps.setString(1, "A");
ps.setString(2, "Security Admin");
ps.setDate(3, startDate);
ps.setDate(4, endDate);
resultSet = ps.executeQuery();
// ... then process the rows as before