Java导出excel的程序里去除字段为null的数据,取出不为空的数据
2个回答
2016-09-30 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
用这个方式占用内存比较小,不过不能控制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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |