
需要设置系统环境变量path的路径:增加JDK的安装路径到path中去。右击桌面我的电脑->高级->环境变量->path中加JDK的安装路径。如C:JKD60\bin;
Java生成CSV文件简单 *** 作实例
CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:
123 1,张三,男2,李四,男3,小红,女Java生成CSV文件(创建与导出封装类)
package comyphompcommonutil;
import javaioBufferedWriter;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
import javaioOutputStreamWriter;
import javautilArrayList;
import javautilIterator;
import javautilLinkedHashMap;
import javautilList;
import javautilMap;
import javaxservlet>
import javaxservlet>
import orgapachecommonsbeanutilsBeanUtils;
import orgjunitTest;
/
Java生成CSV文件
/
public class CSVUtil {
/
生成为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 (!fileexists()) {
filemkdir();
}
// 定义文件名格式并创建
csvFile = FilecreateTempFile(fileName, "csv",
new File(outPutPath));
// UTF-8使正确读取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(csvFile), "GBK"), 1024);
// 写入文件头部
for (Iterator propertyIterator = mapentrySet()iterator(); propertyIterator
hasNext();) {
javautilMapEntry propertyEntry = (javautilMapEntry) propertyIterator
next();
csvFileOutputStream
write("\"" + (String) propertyEntrygetValue() != null (String) propertyEntry
getValue() : "" + "\"");
if (propertyIteratorhasNext()) {
csvFileOutputStreamwrite(",");
}
}
csvFileOutputStreamnewLine();
// 写入文件内容
for (Iterator iterator = exportDataiterator(); iteratorhasNext();) {
Object row = (Object) iteratornext();
for (Iterator propertyIterator = mapentrySet()iterator(); propertyIterator
hasNext();) {
javautilMapEntry propertyEntry = (javautilMapEntry) propertyIterator
next();
/-------------------------------/
//以下部分根据不同业务做出相应的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtilsgetProperty(row,(String) propertyEntrygetKey())) {
if("证件号码"equals(propertyEntrygetValue())){
//避免:身份z号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)
sbContextappend(BeanUtilsgetProperty(row,(String) propertyEntrygetKey()) + "\t");
}else{
sbContextappend(BeanUtilsgetProperty(row,(String) propertyEntrygetKey()));
}
}
csvFileOutputStreamwrite(sbContexttoString());
/-------------------------------/
if (propertyIteratorhasNext()) {
csvFileOutputStreamwrite(",");
}
}
if (iteratorhasNext()) {
csvFileOutputStreamnewLine();
}
}
csvFileOutputStreamflush();
} catch (Exception e) {
eprintStackTrace();
} finally {
try {
csvFileOutputStreamclose();
} catch (IOException e) {
eprintStackTrace();
}
}
return csvFile;
}
/
下载文件
@param response
@param csvFilePath
文件路径
@param fileName
文件名称
@throws IOException
/
public static void exportFile(>
>
throws IOException {
responsesetCharacterEncoding("UTF-8");
responsesetContentType("application/csv;charset=GBK");
responsesetHeader("Content-Disposition", "attachment; filename="
+ new String(fileNamegetBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = responsegetOutputStream();
while ((len = inread(buffer)) > 0) {
outwrite(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
Systemoutprintln(e1);
} finally {
if (in != null) {
try {
inclose();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/
删除该目录filePath下的所有文件
@param filePath
文件目录路径
/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (fileexists()) {
File[] files = filelistFiles();
for (int i = 0; i < fileslength; i++) {
if (files[i]isFile()) {
files[i]delete();
}
}
}
}
/
删除单个文件
@param filePath
文件目录路径
@param fileName
文件名称
/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (fileexists()) {
File[] files = filelistFiles();
for (int i = 0; i < fileslength; i++) {
if (files[i]isFile()) {
if (files[i]getName()equals(fileName)) {
files[i]delete();
return;
}
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void createFileTest() {
List exportData = new ArrayList<Map>();
Map row1 = new LinkedHashMap<String, String>();
row1put("1", "11");
row1put("2", "12");
row1put("3", "13");
row1put("4", "14");
exportDataadd(row1);
row1 = new LinkedHashMap<String, String>();
row1put("1", "21");
row1put("2", "22");
row1put("3", "23");
row1put("4", "24");
exportDataadd(row1);
LinkedHashMap map = new LinkedHashMap();
mapput("1", "第一列");
mapput("2", "第二列");
mapput("3", "第三列");
mapput("4", "第四列");
String path = "d:/export";
String fileName = "文件导出";
File file = CSVUtilcreateCSVFile(exportData, map, path, fileName);
String fileNameNew = filegetName();
String pathNew = filegetPath();
Systemoutprintln("文件名称:" + fileNameNew );
Systemoutprintln("文件路径:" + pathNew );
}
}
//注:BeanUtilsgetProperty(row,(String) propertyEntrygetKey()) + "\t" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。
以上就是关于出现此错误java.lang.ClassNotFoundException: org.apache.commons.beanutils.Converter全部的内容,包括:出现此错误java.lang.ClassNotFoundException: org.apache.commons.beanutils.Converter、需要一份500行的java程序,期末大作业,最好带详细注释。、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)