Skip to content

Commit

Permalink
fix: 修复导出数据到 csv , 当字段中存在逗号时造成的错行问题
Browse files Browse the repository at this point in the history
(cherry picked from commit 1354b03)
  • Loading branch information
Aaron3S committed Oct 23, 2024
1 parent 9ff930d commit 96d3c1a
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jumpserver.chen.framework.session.SessionManager;
import org.jumpserver.chen.framework.ws.io.PacketIO;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;
Expand Down Expand Up @@ -125,6 +126,15 @@ private void fullData(SQLQueryResult result) {
}
}

private static void writeString(BufferedWriter writer, Object object) throws IOException {
var str = object.toString();

if (str.contains(",")) {
str = "\"" + str + "\"";
}
writer.write(str);
}

public void export(String scope) throws SQLException {
var session = SessionManager.getCurrentSession();

Expand All @@ -144,7 +154,7 @@ public void export(String scope) throws SQLException {

if (scope.equals("current")) {
for (Field field : this.data.getFields()) {
writer.write(field.getName());
writeString(writer, field.getName());
writer.write(",");
}
writer.newLine();
Expand All @@ -155,7 +165,7 @@ public void export(String scope) throws SQLException {
writer.write("NULL");
writer.write(",");
} else {
writer.write(row.get(field.getName()).toString());
writeString(writer, row.get(field.getName()));
writer.write(",");
}
}
Expand Down

0 comments on commit 96d3c1a

Please sign in to comment.