javacsv单元格格式_java导出csv格式⽂件的⽅法
本⽂实例为⼤家分享了java导出csv格式⽂件的具体代码,供⼤家参考,具体内容如下
导出csv格式⽂件的本质是导出以逗号为分隔的⽂本数据
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.druid.util.StringUtils;
/**
* ⽂件操作
*/
public class CSVUtils {
/**
* 功能说明:获取UTF-8编码⽂本⽂件开头的BOM签名。
* BOM(Byte Order Mark),是UTF编码⽅案⾥⽤于标识编码的标准标记。例:接收者收到以EF BB BF开头的字节流,就知道是UTF-8编码。
* @return UTF-8编码⽂本⽂件开头的BOM签名
*/
public static String getBOM() {
byte b[] = {(byte)0xEF, (byte)0xBB, (byte)0xBF};
return new String(b);
}
/**
* ⽣成CVS⽂件
* @param exportData
* 源数据List
* @param map
* csv⽂件的列表头map
* @param outPutPath
* ⽂件路径
* @param fileName
* ⽂件名称
* @return
*/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map, String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!ists()) {
file.mkdirs();
}
//定义⽂件名格式并创建
csvFile =new File(outPutPath+fileName+".csv");
// UTF-8使正确读取分隔符","
//如果⽣产⽂件乱码,windows下⽤gbk,linux⽤UTF-8
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( csvFile), "UTF-8"), 1024);
//写⼊前段字节流,防⽌乱码
csvFileOutputStream.write(getBOM());
// 写⼊⽂件头部
for (Iterator propertyIterator = Set().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) ();
csvFileOutputStream.write((String) Value() != null ? (String) Value() : "" ); if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
// 写⼊⽂件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) ();
for (Iterator propertyIterator = Set().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
String str=row!=null?((String)((Map)row).get( Key())):"";
if(StringUtils.isEmpty(str)){
str="";
}else{
placeAll("\"","\"\"");
if(str.indexOf(",")>=0){
str="\""+str+"\"";
}
}
csvFileOutputStream.write(str);
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* ⽣成并下载csv⽂件
* @param response
* @param exportData
* @param map
* @param outPutPath
* @param fileName
* @throws IOException
*/
@SuppressWarnings("rawtypes")
public static void exportDataFile(HttpServletResponse response,List exportData, LinkedHashMap map, String outPutPath,String fileName) throws IOException{
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!ists()) {
file.mkdirs();
}
//定义⽂件名格式并创建
csvFile =new File(outPutPath+fileName+".csv");
ists()){
csvFile.delete();
}
/
/ UTF-8使正确读取分隔符","
//如果⽣产⽂件乱码,windows下⽤gbk,linux⽤UTF-8
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024); //写⼊前段字节流,防⽌乱码
csvFileOutputStream.write(getBOM());
// 写⼊⽂件头部
for (Iterator propertyIterator = Set().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) ();
csvFileOutputStream.write((String) Value() != null ? (String) Value() : "" );
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
// 写⼊⽂件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) ();
for (Iterator propertyIterator = Set().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
String str=row!=null?((String)((Map)row).get( Key())):"";
if(StringUtils.isEmpty(str)){
str="";
}else{
placeAll("\"","\"\"");
if(str.indexOf(",")>=0){
str="\""+str+"\"";
}
}
csvFileOutputStream.write(str);
mkdirs方法if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论