本文目录一览:
- 1、java将查询数据导出成csv文件的问题
- 2、求个java读取csv文件并输出的程序
- 3、java里将从excel读到的数据用csv导出,代码怎么写
- 4、java如何导出csv文件 用户点击导出可以导出到他想保存的地方
java将查询数据导出成csv文件的问题
解释:csv文件实际上就是字符串,之间用“,”进行分割,之后进行的存储。
工具类如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
/**
*
* CSV文件导出工具类
*/
public class CSVUtils {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(ListObject head, ListListObject dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (ListObject row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(ListObject row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
}
求个java读取csv文件并输出的程序
要使用CsvReader,CsvWriter需要下载一个javacsv.jar导入到项目中
package test;import com.csvreader.CsvReader;import com.csvreader.CsvWriter;import java.io.BufferedReader; import java.io.BufferedWriter;import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter;import java.io.IOException; public class readandwrite { public static void main(String[] args) throws IOException { String [] str ={"省","市","区","街","路","里","幢","村","室","园","苑","巷","号"}; String inString = ""; String tmpString = ""; File inFile = new File("C://in.csv"); // 读取的CSV文件 File outFile = new File("C://outtest.csv");//输出的CSV文 try { BufferedReader reader = new BufferedReader(new FileReader(inFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(outFile)); CsvReader creader = new CsvReader(reader, ','); CsvWriter cwriter = new CsvWriter(writer,','); while(creader.readRecord()){ inString = creader.getRawRecord();//读取一行数据 for(int i = 0;i str.length;i++){ tmpString = inString.replace(str[i], "," + str[i] + ","); inString = tmpString; } //第一个参数表示要写入的字符串数组,每一个元素占一个单元格,第二个参数为true时表示写完数据后自动换行 cwriter.writeRecord(inString.split(","), true); //注意,此时再用cwriter.write(inString)方法写入数据将会看到只往第一个单元格写入了数据,“,”没起到调到下一个单元格的作用 //如果用cwriter.write(String str)方法来写数据,则要用cwriter.endRecord()方法来实现换行 //cwriter.endRecord();//换行 cwriter.flush();//刷新数据 } creader.close(); cwriter.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } }}
java里将从excel读到的数据用csv导出,代码怎么写
解释:csv文件实际上就是字符串,之间用“,”进行分割,之后进行的存储。
工具类如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
/**
*
* CSV文件导出工具类
*/
public class CSVUtils {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List head, ListList dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
}
Object-Z
java如何导出csv文件 用户点击导出可以导出到他想保存的地方
这种通常是如下做法:
1:提供查询页面,让用户输入查询条件
2:根据查询条件到数据库去检索,并获取到对应的记录
3:生成csv到本地(可以省略,生成临时文件到tomcat的临时目录)
4:用读取文件,用response写流到客户端