Java 写 Excel 参考参考ABC

package com.example.myproject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelFileGenerator {

	public static void main(String[] args) {

		writeExcel("D:\\test.xls");
	}

	public static void writeExcel(String path) {
		
		String[] extention = {"ABC","DEF","GTY","REW","UYI","NBG"};
		String acctStatus = "O";
		String[] acctInvst = {"FFF","GGG"};
		String parentPrefixString = "100";
		String[] childrenSubfixString = {"YYY","TTT","VVV","NNN","QQQ","PPP"};
		
		HSSFWorkbook  wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("sheet1");
		int countNum = 0;
		String middleString = String.format("%1$05d", 0);
		String parentAccountNum = parentPrefixString+ middleString + String.format("%1$04d", 0);
		
		for (int i = 0; i < 102; i++) {
			System.out.print(i/6);
			if(countNum != i/6) {
				middleString = String.format("%1$05d", i);
				parentAccountNum = parentPrefixString + middleString + String.format("%1$04d", i);
				countNum = i/6;
			}
			HSSFRow row = sheet.createRow(i);
			for (int j = 0; j < 10; j++) {
				HSSFCell cell = row.createCell(j);
				if(j == 0) {
					cell.setCellValue(parentAccountNum);
				}else if(j == 1) {
					cell.setCellValue(acctInvst[i/6%2]); 
				}else if(j == 2) {
					String childrenAccountNum = parentAccountNum.substring(0,9) + childrenSubfixString[i%6];
					cell.setCellValue(childrenAccountNum);
				}else if(j == 3) {
					cell.setCellValue(acctStatus);
				}else if(j == 4) {
					cell.setCellValue(extention[i%6]);
				}else if(j == 5) {
					String accountNum = parentAccountNum.substring(0,9);
					cell.setCellValue(accountNum);
				}else if(j == 6) {
					cell.setCellValue("E");
				}else if (j == 7) {
					cell.setCellValue("EEE");
				}else if (j == 8) {
					cell.setCellValue("GB");
				}else if (j == 9) {
					cell.setCellValue("EEEE");
				}
			}
		}
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		File file = new File(path);// Excel文件生成后存储的位置。
		OutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			wb.write(fos);
			os.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值