实现效果
目录
介绍
1.编写一个方法,传入JSONArray
2.如果是list数据同样道理
3.通过传入的数据进行遍历
4.创建对应的文件
5.创建对应的excel,的行
6.创建行的列内容
7.写入表头
8.写入每一个单元内容
9.校验信息
10.直接用最后的完整文件
创建文件夹
String tempFileDir = "D:/test/09874/";
File parentFileDir = new File(tempFileDir);
if (!parentFileDir.exists()) {
parentFileDir.mkdirs();
}
String uploadFile = tempFileDir+"视频网站数据xx.xlsx";
1.创建方法
2.添加传入值
3.可以扩展添加生成文件地址和生成文件名
4.校验文件夹是否纯在,不纯在,创建,防止应为没有文件夹报错
打开文件
String uploadFile = tempFileDir+"视频网站数据xx.xlsx";
OutputStream fos = new FileOutputStream(uploadFile);
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
XSSFRow row = null;
XSSFCell cell = null;
创建表头
//表头
row = sheet.createRow(0);
String[] names = { "序号", "页面地址", "视频名称" , "上传时间", "视频时长","上传作者主页地址","视频网站地址","视频网站名称"};
for (int index = 0; index < 8; index++) {
cell = row.createCell(index);
cell.setCellValue(names[index]);
}
写入表内容数据
//表数据
for (int i = 0; i < ja.size(); i++) {
String[] zf = ja.getString(i).split(",__,");
row = sheet.createRow(i+1);//创建行
cell = row.createCell(0);//序号
cell.setCellValue(i+1);
cell = row.createCell(1);//内容
cell.setCellValue(zf[0]);
cell = row.createCell(2);
cell.setCellValue(zf[1]);
cell = row.createCell(3);
cell.setCellValue(zf[2]);
cell = row.createCell(4);
cell.setCellValue(zf[3]);
cell = row.createCell(5);
cell.setCellValue(zf[4]);
cell = row.createCell(6);
cell.setCellValue(zf[5]);
cell = row.createCell(7);
cell.setCellValue(zf[6]);
}
workBook.write(fos);
完整代码
/**
* 读取json文件 写入到excel中
* @param ja JSONArray 数据 [{},{}]
*/
public static void spider(JSONArray ja) {
try {
String tempFileDir = "D:/test/09874/";
File parentFileDir = new File(tempFileDir);
if (!parentFileDir.exists()) {
parentFileDir.mkdirs();
}
String uploadFile = tempFileDir+"视频网站数据xx.xlsx";
OutputStream fos = new FileOutputStream(uploadFile);
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
XSSFRow row = null;
XSSFCell cell = null;
//表头
row = sheet.createRow(0);
String[] names = { "序号", "页面地址", "视频名称" , "上传时间", "视频时长","上传作者主页地址","视频网站地址","视频网站名称"};
for (int index = 0; index < 8; index++) {
cell = row.createCell(index);
cell.setCellValue(names[index]);
}
//表数据
for (int i = 0; i < ja.size(); i++) {
String[] zf = ja.getString(i).split(",__,");
row = sheet.createRow(i+1);//创建行
cell = row.createCell(0);//序号
cell.setCellValue(i+1);
cell = row.createCell(1);//内容
cell.setCellValue(zf[0]);
cell = row.createCell(2);
cell.setCellValue(zf[1]);
cell = row.createCell(3);
cell.setCellValue(zf[2]);
cell = row.createCell(4);
cell.setCellValue(zf[3]);
cell = row.createCell(5);
cell.setCellValue(zf[4]);
cell = row.createCell(6);
cell.setCellValue(zf[5]);
cell = row.createCell(7);
cell.setCellValue(zf[6]);
}
workBook.write(fos);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
介绍
1.从上往下可以进行自行扩展
2.主要重点就在于创建文件
3.文件字段位置
4.位置对应上即可
依赖
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.alibaba.fastjson.JSONArray;
ok
持续更新