作者:乔山办公网日期:
返回目录:excel表格制作
我也遇到这个问题,从XLS存为CSV文件后,一打开就是
java.lang.nullpointerException
题主找到问题症结了吗?望告知!谢谢
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
/**
* CSV文件示例程序
* @version 1.0 2014/10/30
*/
public class CsvFileCreator {
public static void main(String[] args) {
CsvFileCreator example = new CsvFileCreator();
example.createCsv();
}
/**
* 创建CSV文件
*/
public void createCsv(){
//换行符
final String NEW_LINE = "\n";
//文件名称
String fileName = "D:\\MyCsv.csv";
try {
//标题头
String title = "序号,姓名,年龄,身高";
StringBuilder csvStr = new StringBuilder();
csvStr.append(title).append(NEW_LINE);
//数据行
for(String csvData : getCsvRows()){
csvStr.append(csvData).append(NEW_LINE);
}
//写文件
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), "GB2312"));
writer.write(csvStr.toString());
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取CSV行数据,各e799bee5baa6e997aee7ad94e78988e69d83336个值之间使用英文逗号分隔
* @return List<String>
*/
private List<String> getCsvRows(){
List<String> result = new ArrayList<String>();
result.add("1,Mr Zhang,20,170");
result.add("2,Mrs Wang,18,175");
result.add("3,Li,30,178");
result.add("4,Zhao,19,175");
result.add("5,Ms Feng,18,165");
return result;
}
}
解释:csv文件实际上就是字符串,之间用“,”进行分割,之后进行的存储。
工具类如下e799bee5baa6e79fa5e98193e4b893e5b19e338:
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, List<List> 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
public static String readExcel(){
StringBuffer sb = new StringBuffer();
File file = new File("D:\\test.xls");
Workbook wb = null;
try {
// 构造Workbook(工作薄)对象
wb = Workbook.getWorkbook(file);
//获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了
Sheet[] sheet = wb.getSheets();
if(sheet!=null&&sheet.length>0){
//对每个工作表进行循环
System.out.println("sheet.length="+sheet.length);
for(int i=0;i<sheet.length;i++){
int rowNum = sheet[i].getRows();
System.out.println("rowNum="+rowNum);
for(int j=0;j<rowNum;j++){
// 得到当前行的所有单元e68a84e8a2ade799bee5baa6e997aee7ad94336格
Cell[] cells = sheet[i].getRow(j);
System.out.println("cells.length="+cells.length);
if(cells!=null&&cells.length>0){
// 对每个单元格进行循环
for(int k=0;k<cells.length;k++){
//读取当前单元格的值
String cellValue = cells[k].getContents();
sb.append(cellValue+"\t");
}
}
sb.append("\r\n");
}
sb.append("\r\n");
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
// 最后关闭资源,释放内存
wb.close();
return sb.toString();
}