Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复导出数据到 csv , 当字段中存在逗号时造成的错行问题 #50

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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