【发布时间】:2019-06-08 22:03:24
【问题描述】:
我已将结果集导出到 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 日期导出更精确的结果,例如,用户在前端页面上插入两个日期范围 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
【问题讨论】:
-
要按日期范围过滤,最好在
where子句中执行此操作。例如:... and LAST_LOGIN between ? and ?"。使用PreparedStatement,这样您就可以安全地将参数传递给sql。 -
我没有硬核,或者因为我将此代码部署到最终用户选择日期范围的服务器。
-
网页输入日期不受控制!
-
我认为最好将每一列相应地存储到arraylist中。当你想写成excel时更容易。
-
我不知道该怎么做!
标签: java ms-access jdbc apache-poi ucanaccess