java输出excel数据的方法示例(一种是到excel,一种是结构化数据到文本文件,可以用editplus之类的看)
来源:互联网 发布:python为什么叫爬虫 编辑:程序博客网 时间:2024/05/11 22:39
最近需要输出大量数据,但是poi 03支持数据有限,于是使用支持数据更多的poi新版本,使用SXSSFWorkbook类,可以输出大量数据。
不过如果需要输出格式,就用excel,后来我发现我只是需要输出结构化数据,不需要复杂的格式设置,那BufferedWriter文件流输出貌似更加合适,简便,高效。
代码我是参考网上的,忘了出处,如有异议,请通知删除,谢谢。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class align_excel{
//注意输出循环的时候,循环体内部千万不能出现新建对象的操作,不然内存很容易溢出;
public static void main(String[] args) throws Exception {
//100行记录存在内存中,这个数字可以改,写入磁盘的数据不能修改;
Workbook wb = new SXSSFWorkbook(100);//XSSFWorkbook(); //or new HSSFWorkbook();三种格式有两种是一样的,hssf是03版本,支持少。
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("d:/xssf-align.xlsx");
Sheet sheet = wb.createSheet();
Row row=sheet.createRow((int)1);
CellStyle cellStyle = wb.createCellStyle();
Cell cell=row.createCell(1);
for (int i=1; i<1000000; i++){
row = sheet.createRow((int) i);
// row.setHeightInPoints(30);
createCell(cellStyle,cell,wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
createCell(cellStyle,cell,wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
createCell(cellStyle,cell,wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
createCell(cellStyle,cell,wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
createCell(cellStyle,cell,wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
createCell(cellStyle,cell,wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
createCell(cellStyle,cell,wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
// wb.write(fileOut);
}
//创建不同类型的单元格
// Workbook wb = new HSSFWorkbook();
//Sheet sheet = wb.createSheet("new sheet");
row = sheet.createRow((int)1000005);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue("这段代码是添加不同类型的单元格格式");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
wb.write(fileOut);
fileOut.close();
wb.close();
// read excel
readexcel();
//这种方法节省内存,存入更多的数据;
writestrems();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param halign the horizontal alignment for the cell.
*/
private static Cell createCell(CellStyle cellStyle,Cell cell,Workbook wb, Row row, short column, short halign, short valign) {
cell= row.createCell(column);
cell.setCellValue("Align It");
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
cell.setCellStyle(cellStyle);
return cell;
}
private static void readexcel(){
// Use a file
// Workbook wb = WorkbookFactory.create(new File("D:/test.xls"));
// Use an InputStream, needs more memory
try {
Workbook wb1 = WorkbookFactory.create(new FileInputStream("D:/test.xlsx"));
Sheet sheet1=wb1.getSheetAt(0);
Row row=sheet1.getRow(0);
Cell cell =row.getCell(0);
System.out.println(cell.getNumericCellValue());
wb1.close();
} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//这个方法更加简便,如果单纯结构化数据输出,不需要excel格式的话,可以用这个方法;
public static void writestrems() throws FileNotFoundException{
File file= new File("D:\\stremExcel.txt");
if (file.isFile()){
file.mkdir();}
FileOutputStream out=new FileOutputStream(file);
OutputStreamWriter osw;
try {
osw = new OutputStreamWriter(out,"GB2312");
BufferedWriter bw =new BufferedWriter(osw);
bw.write("res\tres\tres\tres\tres\t\r\n");
for(int i=0;i<1000000;i++){
bw.write("res\tres\tres\tres\tres\t\r\n");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
0 0
- java输出excel数据的方法示例(一种是到excel,一种是结构化数据到文本文件,可以用editplus之类的看)
- java中导出大量数据到Excel的一种方法
- 一种Oracle Form导入报表数据到Excel的方法
- VB6数据导出到Excel文件,一种设计界面查询条件的方法,一种简单加密方法(改写)
- 导出大量数据到Excel的一种方式
- 导出大量数据到Excel的一种方式
- 导出大量数据到Excel的一种方式
- 往Excel里导入数据的一种方法(dataset-->excel)
- [JAVA]Java导出Excel数据的一种方法(完美实现,包含表格线)
- 数据导入,可以将txt文本文件或excel的内容导入到数据库
- excel表格输出到word中的一种方法
- 极速响应Excel数据报表请求的一种方法
- 数据导出到Excel的方法C#
- c# 数据导入到EXCEL的方法
- 使用Excel“自动化”功能将数据输出到Excel的三种高效方法对比实验
- java将数据输出到Excel中
- JAVA中读取和导出数据到EXCEL的方法
- SAS技巧-将输出数据转向到内存的一种方法
- session,cookie讲的很好
- fork与vfork区别
- Extjs进度条
- Swipe RecyclerView using AndroidSwipeLayout
- 让几个横向排列的浮动子div居中显示的方法
- java输出excel数据的方法示例(一种是到excel,一种是结构化数据到文本文件,可以用editplus之类的看)
- 06-Windows Server 2012 R2 会话远程桌面-标准部署-RD网关(RemoteApp)
- linux常用命令解析
- Oracle-触发器、SQL语句优化
- 【codevs2188】最长上升子序列 标题党系列
- 07-Windows Server 2012 R2 会话远程桌面-标准部署-RD授权
- Maven学习总结
- Android--内存管理机制
- iOS 每日一记之 -----点击验证码按钮 获取验证码