
java导出excel 如何处理字段值为空的不显示
4个回答
展开全部
用这个方式占用内存比较小,不过不能控制excel格式
public class Excel {
public static void main(String[] args) {
try {
String url = "jdbc:oracle:thin:@10.20.1.23:1521:ilisdev";
String username = "uu";
String password = "pp";
Connection conn;
Statement stmt;
ResultSet rs;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql = "select a.appntname,c.riskname,b.name from lcpol a,ldcom b,lmrisk c "
+ "where a.managecom=b.comcode and a.riskcode = c.riskcode";
rs = stmt.executeQuery(sql);
String path = "c:/test.txt";// 数据存放的位置
Excel excel = new Excel(path, new String[] );
excel.makeXLS(rs, 3);
} catch (Exception e) {
e.printStackTrace();
}
}
private BufferedWriter buff = null;
public Excel(String filePath, String[] titles) {
try {
if (filePath == null || filePath.equals("") || titles == null
|| titles.length == 0) {
System.out.println("路径或标题为空!");
} else {
buff = new BufferedWriter(new FileWriter(filePath));
for (int i = 0; i < titles.length; i++) {
buff.write(titles[i] + "\t");
if (i == (titles.length - 1))
buff.write("\r\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void makeXLS(ResultSet rs, int colNum) {
try {
while (rs.next()) {
for (int i = 1; i <= colNum; i++) {
buff.write(rs.getString(i) + "\t");
if (i == colNum) {
buff.write("\r\n");
}
}
}
buff.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Excel {
public static void main(String[] args) {
try {
String url = "jdbc:oracle:thin:@10.20.1.23:1521:ilisdev";
String username = "uu";
String password = "pp";
Connection conn;
Statement stmt;
ResultSet rs;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql = "select a.appntname,c.riskname,b.name from lcpol a,ldcom b,lmrisk c "
+ "where a.managecom=b.comcode and a.riskcode = c.riskcode";
rs = stmt.executeQuery(sql);
String path = "c:/test.txt";// 数据存放的位置
Excel excel = new Excel(path, new String[] );
excel.makeXLS(rs, 3);
} catch (Exception e) {
e.printStackTrace();
}
}
private BufferedWriter buff = null;
public Excel(String filePath, String[] titles) {
try {
if (filePath == null || filePath.equals("") || titles == null
|| titles.length == 0) {
System.out.println("路径或标题为空!");
} else {
buff = new BufferedWriter(new FileWriter(filePath));
for (int i = 0; i < titles.length; i++) {
buff.write(titles[i] + "\t");
if (i == (titles.length - 1))
buff.write("\r\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void makeXLS(ResultSet rs, int colNum) {
try {
while (rs.next()) {
for (int i = 1; i <= colNum; i++) {
buff.write(rs.getString(i) + "\t");
if (i == colNum) {
buff.write("\r\n");
}
}
}
buff.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(obj[m] != null) {
cell.setCellValue(obj[m].toString());
}else {
cell.setCellValue("");
}
cell.setCellValue(obj[m].toString());
}else {
cell.setCellValue("");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
count为字段
在向excel中插入数据时 在SQl中使用nvl(count,0) 将count字段在为空情况下显示0 这样存入excel中的字段也为0
在向excel中插入数据时 在SQl中使用nvl(count,0) 将count字段在为空情况下显示0 这样存入excel中的字段也为0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写判断啊。你的代码是SubString追加的那种吗??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |