用JAVA如何实现写CSV文件 我想要把从DB里读取出来的数据写到指定的CSV文件中,请问该如何实现呢,可以的话,希望各位大侠写个例子出来,不用SERVLET,最好是只用JAVA类可以实现就行。谢谢(^_^)!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.91linux.com/html/article/program/java/20070901/6641.html dawn023 ,你给的那个例子是从指定的CSV文件读数据吧,我要的是,把从DB里取到的数据,写到CSV文件中,有没有上面中类似的方法呀? 1:从数据库查询数据,放到list中,这个应该知道吧。2:再写个方法,把list传过来。参照这个: public static byte[] exportMOEnterChangeExcel(List dataList,String sheetName) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); HSSFWorkbook book = new HSSFWorkbook(); // 创建一个Excel工作区 HSSFCellStyle cellStyleTop = book.createCellStyle();// 标题的样式 HSSFCellStyle cellStyleCondition = book.createCellStyle();// 条件的样式 HSSFCellStyle cellStyle = book.createCellStyle();// 循环数据的样式 cellStyle.setWrapText(false); // 设置标题的样式 HSSFFont f = book.createFont(); // 字体样式类 f.setFontName("宋体"); // 标题字体 f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 标题字体加粗 f.setFontHeightInPoints((short) 15); // 字体大小 cellStyleTop.setFont(f); // 设置字体样式 cellStyleTop.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 标题居中 // 设置条件样式 HSSFFont ff = book.createFont(); // 字体样式类 ff.setFontName("宋体"); // 标题字体 ff.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 标题字体加粗 ff.setFontHeightInPoints((short) 10); // 字体大小 cellStyleCondition.setAlignment(HSSFCellStyle.ALIGN_LEFT); cellStyleCondition.setFont(ff); // 设置循环数据的样式 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM); HSSFCellStyle cellStyle_left = book.createCellStyle(); cellStyle_left.setWrapText(false); cellStyle_left.setAlignment(HSSFCellStyle.ALIGN_LEFT); cellStyle_left.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM); HSSFSheet sheet = book.createSheet(); // 设置sheet显示名称 book.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16); sheet.setDefaultColumnWidth((short) 9); sheet.setDefaultRowHeight((short) 20); // 以下是通过sheet分别设置各列的宽度 sheet.setColumnWidth((short) 0, (short) (256 * 2 * 10));// 第一列 sheet.setColumnWidth((short) 1, (short) (256 * 2 * 6));// 第二列 sheet.setColumnWidth((short) 2, (short) (256 * 2 * 6));// 第三列 sheet.setColumnWidth((short) 3, (short) (256 * 2 * 6));// 第四列 sheet.setPrintGridlines(true); int rowIndex = 0; HSSFRow row = sheet.createRow((short) rowIndex); Region reg = new Region(rowIndex, (short) 0, rowIndex, (short) 11); sheet.addMergedRegion(reg); createCell(row, 0, "标题名称", cellStyleTop); row = sheet.createRow(++rowIndex); reg = new Region(rowIndex, (short) 0, rowIndex, (short) 1);// 合并单元格(0、1列) sheet.addMergedRegion(reg); row = sheet.createRow(++rowIndex); createCell(row, 0, "第一列名称", cellStyle); createCell(row, 1, "第二列名称", cellStyle); createCell(row, 2, "第三列名称", cellStyle); createCell(row, 3, "第四列名称", cellStyle); for (int i = 0; i < dataList.size(); i++) { row = sheet.createRow(++rowIndex);//这是行 createCell(row, 0, 查询出的数据, cellStyle); //这里可以多列,上面定义了四列,这里也写四列的值 } book.write(baos); return baos.toByteArray(); }3:将byte通过inputstream写入文件即可。 楼上的GG,这个方法看起来好像是写到EXCEL文件中的样子啊我要的是写CSV文件 各位GGJJ请帮忙看看吧,小妹感激不尽~~ csv格式的文件就是标题列名,列名,列名,列名,列名字段1,字段2,字段3,字段4,字段5 我知道CSV的格式是什么样的,我想要的是怎么写CSV文件,最好能给我提供一个例子,谢谢~~ 我写的一个例子,还差文件头之类的,因为我的是web不是纯java,剩下的自己补补就行了ColumnPrintInfo cpi;String str = ""; //表头 for (int k = 0; k < titles.size(); k++) { cpi = (ColumnPrintInfo) titles.get(k); out.write(cpi.getTitle().getBytes()); if (k != titles.size() - 1) { out.write(",".getBytes()); } } out.write("\r\n".getBytes()); int tag = 0;//查询出的数据 while (rs.next()) { for (int i = 0; i < titles.size(); i++) { cpi = (ColumnPrintInfo) titles.get(i); if (rs.getObject(cpi.getName()) == null) { out.write("\"\"".getBytes()); } else { str = getCellValue(cpi, rs.getObject(cpi.getName())); if (str != null) { Pattern pat = Pattern.compile("\\d{1}[\\d-/.]*"); Matcher matcher = pat.matcher(str); if (matcher.matches()) { str = "=\"" + str + "\""; } } out.write(str.getBytes()); } if (i != titles.size() - 1) { out.write(",".getBytes()); } } out.write("\r\n".getBytes()); } csv文件就是文本文件了列之间用,隔开就行了 http://sourceforge.net/projects/javacsv/ csv就是名字是CSV的文本文件,你就 当文本写,自己处理下格式,用文件IO就能实现。 用jxl写啊那个好 很好学 jxl?没用过哎,,找找看看撒 CSV 直接写好了,比较简单的。 你照这个写个main方法吧,logFile下看产生CSV文件没有,导出的文件内容有很多符号会影响到CSV文件的格式,视具体处理EX: File file = new File(logFile); //logFile 是路径,就叫做String logFile = 生成路径+想要的文件名+".csv"; FileOutputStream out = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(out, "GB2312"); BufferedWriter bw = new BufferedWriter(osw); //然后就随便你自己去构造了,我写个第一行表示表头,然后第二行起循环 bw.write("a1" + "," + "a2" + "," + "a3" + "\r\n");//请注意,CSV默认是已逗号","分隔单元格的。这里是表头 List list = service.getYouDb;//取出的数据 for(int i = 0;i<list.size();i++) { Object objs = (Object) list.get(i);//hibernate mmessage1 = objs.get...... mmessage2 = objs.get...... mmessage3 = objs.get...... bw.write(mmessage1 + "," + message2 + "," + message3+ "\r\n");} bw.close(); osw.close(); out.close(); Jpanel 画图多出了个按钮 服务器端逻辑判断写法 大家帮忙看看为什么我的面板不显示内容 50分求解一个很小白的问题,在线等待,马上给分 页面中iframe打印的问题 代码块中有return语句,但是jBuilder老是说需要返回值。 这是怎么回事,字符串为什么显示不出来? 那位说下CSDN这几天在搞啥哦? 连接SQL server 的问题 CORBA问题 java一个关于this和toString()的问题 在线送分,简单问题
我要的是,把从DB里取到的数据,写到CSV文件中,
有没有上面中类似的方法呀?
2:再写个方法,把list传过来。参照这个:
public static byte[] exportMOEnterChangeExcel(List dataList,String sheetName)
throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HSSFWorkbook book = new HSSFWorkbook(); // 创建一个Excel工作区 HSSFCellStyle cellStyleTop = book.createCellStyle();// 标题的样式
HSSFCellStyle cellStyleCondition = book.createCellStyle();// 条件的样式
HSSFCellStyle cellStyle = book.createCellStyle();// 循环数据的样式
cellStyle.setWrapText(false); // 设置标题的样式
HSSFFont f = book.createFont(); // 字体样式类
f.setFontName("宋体"); // 标题字体
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 标题字体加粗
f.setFontHeightInPoints((short) 15); // 字体大小
cellStyleTop.setFont(f); // 设置字体样式
cellStyleTop.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 标题居中 // 设置条件样式
HSSFFont ff = book.createFont(); // 字体样式类
ff.setFontName("宋体"); // 标题字体
ff.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 标题字体加粗
ff.setFontHeightInPoints((short) 10); // 字体大小
cellStyleCondition.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cellStyleCondition.setFont(ff); // 设置循环数据的样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM); HSSFCellStyle cellStyle_left = book.createCellStyle();
cellStyle_left.setWrapText(false);
cellStyle_left.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cellStyle_left.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM); HSSFSheet sheet = book.createSheet(); // 设置sheet显示名称
book.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16); sheet.setDefaultColumnWidth((short) 9);
sheet.setDefaultRowHeight((short) 20); // 以下是通过sheet分别设置各列的宽度
sheet.setColumnWidth((short) 0, (short) (256 * 2 * 10));// 第一列
sheet.setColumnWidth((short) 1, (short) (256 * 2 * 6));// 第二列
sheet.setColumnWidth((short) 2, (short) (256 * 2 * 6));// 第三列
sheet.setColumnWidth((short) 3, (short) (256 * 2 * 6));// 第四列 sheet.setPrintGridlines(true); int rowIndex = 0; HSSFRow row = sheet.createRow((short) rowIndex);
Region reg = new Region(rowIndex, (short) 0, rowIndex, (short) 11);
sheet.addMergedRegion(reg); createCell(row, 0, "标题名称", cellStyleTop); row = sheet.createRow(++rowIndex);
reg = new Region(rowIndex, (short) 0, rowIndex, (short) 1);// 合并单元格(0、1列)
sheet.addMergedRegion(reg); row = sheet.createRow(++rowIndex);
createCell(row, 0, "第一列名称", cellStyle);
createCell(row, 1, "第二列名称", cellStyle);
createCell(row, 2, "第三列名称", cellStyle);
createCell(row, 3, "第四列名称", cellStyle); for (int i = 0; i < dataList.size(); i++)
{
row = sheet.createRow(++rowIndex);//这是行
createCell(row, 0, 查询出的数据, cellStyle);
//这里可以多列,上面定义了四列,这里也写四列的值
} book.write(baos); return baos.toByteArray();
}
3:将byte通过inputstream写入文件即可。
我要的是写CSV文件
小妹感激不尽~~
列名,列名,列名,列名,列名
字段1,字段2,字段3,字段4,字段5
我想要的是怎么写CSV文件,最好能给我提供一个例子,谢谢~~
ColumnPrintInfo cpi;
String str = "";
//表头
for (int k = 0; k < titles.size(); k++) {
cpi = (ColumnPrintInfo) titles.get(k);
out.write(cpi.getTitle().getBytes());
if (k != titles.size() - 1) {
out.write(",".getBytes());
}
}
out.write("\r\n".getBytes());
int tag = 0;
//查询出的数据
while (rs.next()) {
for (int i = 0; i < titles.size(); i++) {
cpi = (ColumnPrintInfo) titles.get(i);
if (rs.getObject(cpi.getName()) == null) {
out.write("\"\"".getBytes());
} else {
str = getCellValue(cpi, rs.getObject(cpi.getName()));
if (str != null) {
Pattern pat = Pattern.compile("\\d{1}[\\d-/.]*");
Matcher matcher = pat.matcher(str);
if (matcher.matches()) {
str = "=\"" + str + "\"";
}
}
out.write(str.getBytes());
}
if (i != titles.size() - 1) {
out.write(",".getBytes());
}
}
out.write("\r\n".getBytes());
}
那个好 很好学
找找看看撒
EX:
File file = new File(logFile); //logFile 是路径,就叫做String logFile = 生成路径+想要的文件名+".csv";
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out, "GB2312"); BufferedWriter bw = new BufferedWriter(osw);
//然后就随便你自己去构造了,我写个第一行表示表头,然后第二行起循环 bw.write("a1" + "," + "a2" + "," + "a3" + "\r\n");//请注意,CSV默认是已逗号","分隔单元格的。这里是表头 List list = service.getYouDb;//取出的数据
for(int i = 0;i<list.size();i++) {
Object objs = (Object) list.get(i);//hibernate
mmessage1 = objs.get......
mmessage2 = objs.get......
mmessage3 = objs.get......
bw.write(mmessage1 + "," + message2 + "," + message3+ "\r\n");
}
bw.close();
osw.close();
out.close();