java 代码创建的 csv 正在将所有数据写入同一行
csv created by java code is writing all the data in the same row
这是一个 beanshell 代码,所以可能看起来有些东西
对于 java 开发人员来说很奇怪。 emailFileAttachment 函数是一个 sailpoint API,我正在使用的一个工具。我的问题是我放入地图的数据被放在 excel 文件中的一行中。我放入地图的 header ("Application, Num_entitlement") 没有打印在 CSV 文件的第一行。谁能帮帮我。这是我的代码:
import sailpoint.object.Application;
import sailpoint.object.Identity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.sql.DataSource;
import java.sql.SQLException;
import sailpoint.server.Environment;
import javax.sql.DataSource;
import java.sql.ResultSet;
import sailpoint.api.SailPointContext;
import java.io.BufferedWriter;
import java.io.IOExceptoin;
import java.io.FileWriter;
import sailpoint.object.EmailTemplate;
import sailpoint.object.EmailOptions;
import java.io.File;
import java.io.FileInputStream;
import sailpoint.object.EmailFileAttachment;
import java.util.HashMap;
import sailpoint.tools.Util;
String query = "SELECT app.name as application, count(*) as num_entitlements FROM spt_application as app, spt_identity_entitlement as ent WHERE app.id = ent.application GROUP BY app.name";
HashMap info = new HashMap();
info.put("Application ", "Num_Entitlement");
PreparedStatement getEntitlement_Num = null;
Connection conn = null;
/*
public static byte[] readFiletoByteArray(File file)
{
FileInputStream fileInputStream = null;
byte[] byteFile = new byte[(int) file.length()];
try
{
fileInputStream = new FileInputStream(file);
fileInputStream.read(byteFile);
fileInputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return byteFile;
}
*/
try{
// Prepared Statements
Environment e = Environment.getEnvironment();
DataSource src = e.getSpringDataSource();
//System.out.println("DataSource: " + src.toString());
conn = src.getConnection();
//System.out.println("Connection: " + conn);
getEntitlement_Num = conn.prepareStatement(query);
ResultSet rs = getEntitlement_Num.executeQuery();
//System.out.println("starting RS");
while(rs.next()) {
String appName = rs.getString("application");
int no_ent = rs.getInt("num_entitlements");
info.put(appName , no_ent);
}
System.out.println("finished RS");
}catch(SQLException e){
log.error( e.toString());
} finally {
if (getEntitlement_Num!= null) {
getEntitlement_Num.close();
}
if(conn != null) {
conn.close();
}
}
//I am using sailpoint APIs for the code below.
String emailDest = "//email address here";
EmailTemplate et = new EmailTemplate();
et.setFrom("//email address here");
et.setBody("Please find an attached CSV file that has the list of all applications in IIQ and their number of Entitlements");
et.setTo(emailDest);
et.setSubject("Entitlement count for each application in IIQ");
EmailOptions ops = new EmailOptions(emailDest,null);
String strInfo = Util.mapToString(info);
byte[] fileData = strInfo.getBytes();
EmailFileAttachment attachment = new EmailFileAttachment( "EntitlementCount.csv", EmailFileAttachment.MimeType.MIME_CSV, fileData );
ops.addAttachment(attachment);
context.sendEmailNotification(et, ops);
//System.out.println("email sent");
return "Success";
您应该在与记录迭代相同的循环中使用 StringBuilder,然后从 String 构建器制定附件。
我认为 Utility.MapToString 与 chashma 是根本原因。
info
是一个 HashMap
,这意味着不能保证您可以按照输入的顺序提取数据。因此您的 header "Application" 可能在 CSV 文件中不排在第一位。相反,使用维护顺序的东西,例如 Tuple
objects 的 ArrayList
(您自己编写的包含两个字符串变量的 class)。
Util.mapToString(info) 是如何工作的?我们需要查看它以便调查换行问题。
Util.mapToString() 只会将映射转换为字符串。
尝试将您的 collection 更改为 list of list{app, count} 和
遍历列表以生成字符串。
Util.listToCsv() 或 Util.listToQuotedCsv() 方法将有助于准备 csv 字符串。
希望对您有所帮助。
这是一个 beanshell 代码,所以可能看起来有些东西 对于 java 开发人员来说很奇怪。 emailFileAttachment 函数是一个 sailpoint API,我正在使用的一个工具。我的问题是我放入地图的数据被放在 excel 文件中的一行中。我放入地图的 header ("Application, Num_entitlement") 没有打印在 CSV 文件的第一行。谁能帮帮我。这是我的代码:
import sailpoint.object.Application;
import sailpoint.object.Identity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.sql.DataSource;
import java.sql.SQLException;
import sailpoint.server.Environment;
import javax.sql.DataSource;
import java.sql.ResultSet;
import sailpoint.api.SailPointContext;
import java.io.BufferedWriter;
import java.io.IOExceptoin;
import java.io.FileWriter;
import sailpoint.object.EmailTemplate;
import sailpoint.object.EmailOptions;
import java.io.File;
import java.io.FileInputStream;
import sailpoint.object.EmailFileAttachment;
import java.util.HashMap;
import sailpoint.tools.Util;
String query = "SELECT app.name as application, count(*) as num_entitlements FROM spt_application as app, spt_identity_entitlement as ent WHERE app.id = ent.application GROUP BY app.name";
HashMap info = new HashMap();
info.put("Application ", "Num_Entitlement");
PreparedStatement getEntitlement_Num = null;
Connection conn = null;
/*
public static byte[] readFiletoByteArray(File file)
{
FileInputStream fileInputStream = null;
byte[] byteFile = new byte[(int) file.length()];
try
{
fileInputStream = new FileInputStream(file);
fileInputStream.read(byteFile);
fileInputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return byteFile;
}
*/
try{
// Prepared Statements
Environment e = Environment.getEnvironment();
DataSource src = e.getSpringDataSource();
//System.out.println("DataSource: " + src.toString());
conn = src.getConnection();
//System.out.println("Connection: " + conn);
getEntitlement_Num = conn.prepareStatement(query);
ResultSet rs = getEntitlement_Num.executeQuery();
//System.out.println("starting RS");
while(rs.next()) {
String appName = rs.getString("application");
int no_ent = rs.getInt("num_entitlements");
info.put(appName , no_ent);
}
System.out.println("finished RS");
}catch(SQLException e){
log.error( e.toString());
} finally {
if (getEntitlement_Num!= null) {
getEntitlement_Num.close();
}
if(conn != null) {
conn.close();
}
}
//I am using sailpoint APIs for the code below.
String emailDest = "//email address here";
EmailTemplate et = new EmailTemplate();
et.setFrom("//email address here");
et.setBody("Please find an attached CSV file that has the list of all applications in IIQ and their number of Entitlements");
et.setTo(emailDest);
et.setSubject("Entitlement count for each application in IIQ");
EmailOptions ops = new EmailOptions(emailDest,null);
String strInfo = Util.mapToString(info);
byte[] fileData = strInfo.getBytes();
EmailFileAttachment attachment = new EmailFileAttachment( "EntitlementCount.csv", EmailFileAttachment.MimeType.MIME_CSV, fileData );
ops.addAttachment(attachment);
context.sendEmailNotification(et, ops);
//System.out.println("email sent");
return "Success";
您应该在与记录迭代相同的循环中使用 StringBuilder,然后从 String 构建器制定附件。
我认为 Utility.MapToString 与 chashma 是根本原因。
info
是一个 HashMap
,这意味着不能保证您可以按照输入的顺序提取数据。因此您的 header "Application" 可能在 CSV 文件中不排在第一位。相反,使用维护顺序的东西,例如 Tuple
objects 的 ArrayList
(您自己编写的包含两个字符串变量的 class)。
Util.mapToString(info) 是如何工作的?我们需要查看它以便调查换行问题。
Util.mapToString() 只会将映射转换为字符串。 尝试将您的 collection 更改为 list of list{app, count} 和 遍历列表以生成字符串。 Util.listToCsv() 或 Util.listToQuotedCsv() 方法将有助于准备 csv 字符串。
希望对您有所帮助。