展开全部
package cn.com.sunjapan.university.web;
import java.util.ArrayList;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.sunjapan.comlib.exception.PJException;
import cn.com.sunjapan.framework.core.misc.ChainedException;
public class ParseCsvUtil {
transient private static Log log = LogFactory.getLog(ParseCsvUtil.class);
/** 屏蔽构造函数 */
private ParseCsvUtil() {
}
/** 单子模式静态变量 */
private static ParseCsvUtil instance;
/**
* 获取静态方e68a843231313335323631343130323136353331333332623335法
*
* @return 静态变量
*/
public static synchronized ParseCsvUtil getInstance() {
if (instance == null) {
instance = new ParseCsvUtil();
}
return instance;
}
/**
* 得到文件头的数据
*
* @param csvdata
* 头文件信息
* @return 头文件信息
*/
public ArrayList parseCsvHeadData(String[][] csvdata) {
if (log.isDebugEnabled()) {
log.debug("now we enter parseCsvHeadData of ParseCsvFileManager");
}
ArrayList csvHeadList = new ArrayList();
for (int i = 0; i
csvHeadList.add(csvdata[0][i]);
}
return csvHeadList;
}
/**
* 得到文件内容的list
*
* @param csvdata
* 文件内容
* @return 文件内容
*/
public ArrayList parseCsvData(String[][] csvdata) {
if (log.isDebugEnabled()) {
log.debug("now we enter parseCsvData of ParseCsvFileManager");
}
ArrayList csvList = new ArrayList();
for (int i = 1; i
csvList.add(csvdata[i]);
}
return csvList;
}
/**
* 解析csv文件一行数据
*
* @param csvLine
* csv文件的一行数据
* @return 解析后的字符串数组
* @throws ChainedException
* 抛出异常
*/
public static String[] parseCsvLine(String csvLine) throws ChainedException {
if (log.isDebugEnabled()) {
log.debug("now we enter parseCsvLine of CSVFileParse");
}
String[] retArray = null;
if (csvLine == null || csvLine.trim().length() <= 0) {
return null;
}
StringBuffer sbuf = new StringBuffer();
Vector result = new Vector();
boolean beginWithQuote = false;
int length = csvLine.length();
for (int i = 0; i
char ch = csvLine.charAt(i);
if (ch == '\"') {
if (beginWithQuote) {
i++;
if (i >= length) {
result.addElement(sbuf.toString());
sbuf = new StringBuffer();
beginWithQuote = false;
} else {
ch = csvLine.charAt(i);
if (ch == '\"') {
sbuf.append(ch);
} else if (ch == ',') {
result.addElement(sbuf.toString());
sbuf = new StringBuffer();
beginWithQuote = false;
} else {
throw new ChainedException(
"Single double-quote char mustn't exist in filed "
+ (result.size() + 1)
+ " while it is begined with quote\nchar at:"
+ i);
}
}
} else if (sbuf.length() == 0) {
beginWithQuote = true;
} else {
throw new ChainedException(
"Quote cannot exist in a filed which doesn't begin with quote!\nfield:"
+ (result.size() + 1));
}
} else if (ch == ',') {
if (beginWithQuote) {
sbuf.append(ch);
} else {
result.addElement(sbuf.toString());
sbuf = new StringBuffer();
beginWithQuote = false;
}
} else {
sbuf.append(ch);
}
}
if (sbuf.length() != 0) {
if (beginWithQuote) {
throw new ChainedException(
"last field is begin with but not end with double quote");
} else {
result.addElement(sbuf.toString());
}
}
retArray = new String[result.size()];
for (int i = 0; i
retArray[i] = (String) result.elementAt(i);
}
return retArray;
}
/**
* 导出文件
*
* @param request
* HttpServletRequest
* @return 导出文件
* @throws ChainedException
* ChainedException
*/
public ArrayList outFileList(String[][] content)
throws ChainedException {
if (log.isDebugEnabled()) {
log.debug("now we enter outFileList of ParseCsvFileManager");
}
ArrayList list = new ArrayList ();
try {
if (content.length > 0) {
for (int i = 0; i
String lineValue = "";
for(int x = 0; x
if(x == content[i].length - 1){
lineValue += content[i][x];
}else {
lineValue += content[i][x] + ",";
}
}
// 解析字符串把null替换成空的
String[] valueList = parseCsvLine(lineValue
.replaceAll("null", "")
+ ",");
list.add(valueList);
}
}
} catch (PJException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return list;
}
}