자바로 엑셀 줄,열 만들기
-
게시물 수정 , 삭제는 로그인 필요
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.Scanner;
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;
public class RegisterProductTest {
public static void main(String[] args) {
excelWriter("C:\\work\\exA\\a01\\売上35.csv");
}
static void excelWriter(String filePath) {
XSSFWorkbook xworkbook = null;
XSSFSheet xSheet = null;
XSSFRow xRow = null;
XSSFCell xCell = null;
DecimalFormat df = new DecimalFormat("#,###");
Scanner scan = new Scanner(System.in);
DecimalFormat df2 = new DecimalFormat("000");
System.out.println("商品コード,商品名,単価,販売数の順番に入力してください。");
int code1 =0;
System.out.println("商品コード"+df2.format(++code1));
String name1 = scan.next();
System.out.println("商品名:"+name1);
int money1 = scan.nextInt();
System.out.println("単価 :"+money1);
System.out.println();
System.out.println("商品コード"+df2.format(++code1));
String name2 = scan.next();
System.out.println("商品名:"+name2);
int money2 = scan.nextInt();
System.out.println("単価 :"+money2);
try {
xworkbook = new XSSFWorkbook();
xSheet = xworkbook.createSheet("sheet1");
xRow = xSheet.createRow(0);
xCell = xRow.createCell(0);
xCell.setCellValue("商品コード");
xCell = xRow.createCell(1);
xCell.setCellValue("商品名");
xCell = xRow.createCell(2);
xCell.setCellValue("単価");
xRow = xSheet.createRow(1);
xCell = xRow.createCell(0);
xCell.setCellValue("商品コード"+df2.format(code1));
xCell = xRow.createCell(1);
xCell.setCellValue(name1);
xCell = xRow.createCell(2);
xCell.setCellValue(df.format(money1));
xRow = xSheet.createRow(2);
xCell = xRow.createCell(0);
xCell.setCellValue("商品コード"+df2.format(code1));
xCell = xRow.createCell(1);
xCell.setCellValue(name2);
xCell = xRow.createCell(2);
xCell.setCellValue(df.format(money2));
File file = new File(filePath);
FileOutputStream fos = new FileOutputStream(file);
xworkbook.write(fos);
if(fos != null)
fos.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
이방식은 손으로 직접 열 줄 만들어서 하는방식인데
제가 데이터를 입력한만큼 열하고 줄이 늘어나는 방법은 없나요??
#자바로 엑셀 읽기