我有一个csv文件,我在其中将数据库查询到一个csv文件。但是由于某些原因,数据不准确。正如您在步骤_1列的“我的结果”下图中所看到的,数据看起来很准确,但是需要将$ 4转移到步骤_2列中。有什么办法可以帮助我用下面的代码解决这个问题?谢谢
我的结果
我想完成的事情
public void exportData() throws IOException, SQLException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseContentType("text/csv");
externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"all_salaries.csv\"");
OutputStream out = externalContext.getResponseOutputStream();
Writer writer = new OutputStreamWriter(out);
String sql = "SELECT "
+ " job_class_cd \"JOB CLASS\", \n"
+ " pay_grade \"PAY GRADE\",\n"
+ " job_class_title, "
+ " mou,"
+ " mou_description,"
+ " nvl(REGEXP_REPLACE(minimum_annual, '\\.[0-9]*', ''), ' ') \"MINIMUM ANNUAL\",\n"
+ " nvl(REGEXP_REPLACE(maximum_annual, '\\.[0-9]*', ''), ' ') \"MAXIMUM ANNUAL\",\n"
+ " nvl(salary_range || CASE WHEN min_step IS NULL THEN '' ELSE ' (' || to_char(MIN_STEP) || ')' END, ' ') \"SALARY RANGE (MIN STEP)\",\n"
+ " nvl(rate_type, ' ') \"RATE TYPE\",\n"
+ " nvl(flat_rate, ' ') \"FLAT RATE\",\n"
+ " nvl(per_event_rate, ' ') \"PER EVENT RATE\",\n"
+ " step_1,"
+ " step_2,"
+ " step_3,"
+ " step_4,"
+ " step_5,"
+ " step_6,"
+ " step_7,"
+ " step_8,"
+ " step_9,"
+ " step_10,"
+ " step_11,"
+ " step_12,"
+ " step_13,"
+ " step_14,"
+ " step_15"
+ " FROM myBean.va_v_vpay"
+ " ORDER BY job_class_title, job_class_cd, pay_grade, rate_type desc";
// MARK: CSV content header
try {
SqlRowSet rs = dataUtilityService.getJdbcTemplate().queryForRowSet(sql);
writer.write("JOB CLASS, PAY GRADE, JOB_CLASS_TITLE");
writer.write(", MOU");
writer.write(", MOU_DESCRIPTION");
writer.write(", MINIMUM ANNUAL, MAXIMUM ANNUAL");
writer.write(", SALARY RANGE (MIN STEP), RATE TYPE");
writer.write(", FLAT RATE, PER EVENT RATE");
writer.write(", STEP_1, STEP_2");
writer.write(", STEP_3, STEP_4");
writer.write(", STEP_5, STEP_6");
writer.write(", STEP_7, STEP_8");
writer.write(", STEP_9, STEP_10");
writer.write(", STEP_11, STEP_12");
writer.write(", STEP_13, STEP_14");
writer.write(", STEP_15");
writer.write(",");
writer.write("\n");
while (rs.next()) {
writer.write(rs.getString(1));
writer.write(",");
writer.write(rs.getString(2));
writer.write(",");
writer.write(rs.getString(3));
writer.write(",");
writer.write(rs.getString(4));
writer.write(",");
writer.write(rs.getString(5));
writer.write(",");
writer.write('"');
writer.write(rs.getString(6));
writer.write('"');
writer.write(",");
writer.write('"');
writer.write(rs.getString(7));
writer.write('"');
writer.write(",");
writer.write(rs.getString(8));
writer.write(",");
writer.write(rs.getString(9));
writer.write(",");
writer.write(rs.getString(10));
writer.write(",");
writer.write(rs.getString(11));
writer.write(",");
writer.write('"');
writer.write(rs.getString(12));
writer.write(",");
writer.write('"');
writer.write(rs.getString(13));
writer.write('"');
writer.write(rs.getString(14));
writer.write(rs.getString(15));
writer.write(rs.getString(16));
writer.write(rs.getString(17));
writer.write(rs.getString(18));
writer.write(rs.getString(19));
writer.write(rs.getString(20));
writer.write(rs.getString(21));
writer.write(rs.getString(22));
writer.write(rs.getString(23));
writer.write(rs.getString(24));
writer.write(rs.getString(25));
writer.write(rs.getString(26));
writer.write(",");
writer.write("\n");
}
} finally {
if (writer != null) {
// MARK: Close and flush the OutputStream
writer.flush();
writer.close();
}
}
facesContext.responseComplete();
}
文本编辑的结果:
STEP_1,STEP_2,STEP_3,STEP_4,STEP_5,STEP_6,STEP_7,STEP_8,STEP_9,STEP_10,STEP_11,STEP_12,STEP_13,STEP_14,STEP_15,
9206,0,DIRECTOR,36,TEST,“”,“”,5736(1),小时,,“ $ 57.36”,“ $ 58.94”,$ 60.56,$ 62.23,$ 65.70,$ 69.37,$ 73.23,$ 77.31,$ 79.44,$ 81.62,$ 83.87, $ 86.18,,
9206,0,DIRECTOR,TEST UNI,“”,“”,5736(1),每两周一次,“” $ 4,588.80“,” $ 4,715.20“,$ 4,844.80,$ 4,978.40,$ 5,256.00,$ 5,549.60,$ 5,858.40,$ 6,184.80,$ 6,355.60,$ 6,355.60, , 9206,0,“ $ 119,767”,“ $ 179,943”,5736(1),年度“,” $ 119,767.68“,” $ 123,066.72“,$ 126,449.28,$ 129,936.24,$ 137,181.60,$ 144,844.56,$ 152,904.24,$ 161,423.28,$ 165,870.56,$ 165,870.72, 2208,0,TRAINEE,00,NON,“ $ 41,760”,“ $ 41,760” ,,小时,$ 20.00,“”,“”,,,,,,,,,,,,,
答案 0 :(得分:0)
如果需要将数字作为格式字符串,则需要引用所有包含逗号的字符串。
// Takes some value and coverts it to a quoted string.
String getQuotedCell(Object o){
StringBuilder sb = new StringBuilder("\"");
sb.append(o).append("\",");
return sb.toString();
}
此处示例Repl.it
我建议您虽然只输出数字值而不是字符串。电子表格程序可以很容易地将数字格式化为美元金额。
有点像这样。
while (rs.next()) {
StringBuilder sb = new StringBuilder();
for (int i =1; i <= 26; i++) {
sb.append(rs.getObject(i)).append(",")
}
sb.append("\n");
writer.write(sb.toString()); // write row.