嗨,我正在尝试从端点动态构建excel数据,并使用java JAX-RS将其返回给客户端
现在,我有一个简单的按钮,该按钮调用我的函数以到达该Java端点,并且我试图使用StringBuilder来构建要返回的数据,然后将其放到CSV XLS或其他任何东西并返回给客户。
@GET
@Path("/excelregisterbycountry")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response excelRegister(
@QueryParam("ModelNumber") String modelNbr,
@DefaultValue("US") @QueryParam("Country") String country) {
String myQuery = "";
try {
database = new EstateDatabaseManager(azureHost, azureDB);
} catch (Exception e) {
connectionFailed = true;
slrLog.write(DB_CONNECT_FAIL + e.getMessage());
}
if (!connectionFailed) {
myQuery = "SELECT distinct spr.country_code, spr.store_nbr, spr.register_nbr, spr.os_version_nbr, sco.adkversion, sco.termType, RTRIM(spr.bios_version_nbr), RTRIM(rt.model_nbr), st.store_type_desc, psi.os_level_id, psi.gsa_level_id, RTRIM(psi.self_checkout_type_cd), psi.si_level, psi.pf_level " +
"FROM str_pos_register2 spr " +
"JOIN register_type rt " +
"ON rt.register_type_cd = spr.register_type_cd " +
"join pos_store_info3 psi " +
"ON psi.country_code = spr.country_code " +
"AND psi.store_nbr = spr.store_nbr " +
"join store_type as st " +
"on psi.store_type_cd = st.store_type_cd " +
"Left join sco.daily sco " +
"on spr.register_nbr = sco.registernbr and spr.country_code = sco.countryCode and spr.store_nbr = sco.storeNbr and cast(spr.last_check_ts as date) = sco.reportDate " +
"WHERE spr.country_code <> '??' " +
"AND psi.store_status_code = 0 " +
"AND rt.model_nbr = ? " +
"AND spr.country_code = ? " +
"AND spr.last_check_ts > (CURRENT_TIMESTAMP -7) "+
"ORDER BY spr.store_nbr";
}
try {
StringBuilder sb = new StringBuilder();
sb.append("Country Code");
sb.append(",");
sb.append("Store Number");
sb.append(",");
sb.append("Register NUmber");
sb.append(",");
sb.append("OS Version");
sb.append(",");
sb.append("ADK Version");
sb.append(",");
sb.append("Term Type");
sb.append(",");
sb.append("Bios Version");
sb.append(",");
sb.append("Model Number");
sb.append(",");
sb.append("Store Type");
sb.append(",");
sb.append("OS Level ID");
sb.append(",");
sb.append("GSA Level ID");
sb.append(",");
sb.append("Self Checkout Ind");
sb.append(",");
sb.append("siLevel");
sb.append(",");
sb.append("pfLevel");
sb.append(",");
sb.append("\n");
trouxInfoScoRegisterObject[] dataObject = null;
List<trouxInfoScoRegisterObject> dataStore = new ArrayList<trouxInfoScoRegisterObject>();
trouxInfoScoRegisterObject trouxRegister = null;
PackageType<trouxInfoScoRegisterObject> t = new PackageType<>();
database.prepareSQL(myQuery);
database.addParameter(modelNbr, EstateDatabaseManager.TYPE_STRING);
database.addParameter(country, EstateDatabaseManager.TYPE_STRING);
database.query();
while (database.nextRow()) {
trouxRegister = new trouxInfoScoRegisterObject();
String countryCode = database.getColumn(1);
Integer storeNumber = database.getColumnInt(2);
Integer registerNumber = database.getColumnInt(3);
String osVersionNbr = database.getColumn(4);
String adkVersion = database.getColumn(5);
String termType = database.getColumn(6);
String biosVersionNbr = database.getColumn(7);
String ModelNumber = database.getColumn(8);
String storeType = database.getColumn(9);
String osLevelId = database.getColumn(10);
String gsaLevelId = database.getColumn(11);
String selfCheckoutInd = database.getColumn(12);
String siLevel = database.getColumn(13);
String pfLevel = database.getColumn(14);
sb.append(countryCode);
sb.append(",");
sb.append(storeNumber);
sb.append(",");
sb.append(registerNumber);
sb.append(",");
sb.append(osVersionNbr);
sb.append(",");
sb.append(adkVersion);
sb.append(",");
sb.append(termType);
sb.append(",");
sb.append(biosVersionNbr);
sb.append(",");
sb.append(ModelNumber);
sb.append(",");
sb.append(storeType);
sb.append(",");
sb.append(osLevelId);
sb.append(",");
sb.append(gsaLevelId);
sb.append(",");
sb.append(selfCheckoutInd);
sb.append(",");
sb.append(siLevel);
sb.append(",");
sb.append(pfLevel);
sb.append(",");
sb.append("\n");
}
FileWriter file = new FileWriter("test.csv");
file.append(sb.toString());
ResponseBuilder rb = Response.ok(file);
rb.header("content-disposition", "attachment; filename=rest-test.xlsx");
return rb.build();
}catch(Exception e) {
slrLog.write(NEXT_ROW_ERR + e.getMessage());
}
我正在尝试构建所有内容,然后将其返回给客户端,我认为我的问题是我不需要FileWriter,该FileWriter希望将StringBuilder写入不需要的文件,我只想返回StringBuilder内置为CSV格式。一切都会顺利进行,直到返回说找不到源的返回,这是因为,当然,我在任何地方都没有test.csv。我只是尝试做response.ok(sb.tostring())
,但是它只是将我的数据以纯文本格式返回给客户端,而不是CSV或excel文件。我将不胜感激。
答案 0 :(得分:1)
不要仅因为application/octet-stream
是文件下载而使用。根据实际数据使用正确的MIME类型。如果它是CSV文件,请使用text/csv
,因为它是标准的 1
@Produces("text/csv")
对于实体,只需返回String。这就是您需要做的。不需要File
或FileWriter
或任何类型的中间存储。
return Response.ok(sb.toString())
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=test.csv")
.build();
就是这样。很简单。
请注意,CSV和Excel文件不是同一件事。 CSV文件是文本文件,而Excel文件是二进制文件。您的数据是CSV数据,所以我不确定为什么您将文件名标记为.xls
文件。应该是.csv