通过java程序,将使用者输入的信息存进Excel表中
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.Scanner;
public class POIWriteExcel {
public static void main(String[] args) throws IOException {
Workbook book=new XSSFWorkbook();//创建Excel工作簿
Scanner in=new Scanner(System.in);
Sheet sheet=book.createSheet();//创建一个工作表
int Rownum;//行数
int CellNum;//单元格数
System.out.println("请输入要创建的行数:");
Rownum=in.nextInt();
System.out.println("请输入要创建的单元格数:");
CellNum=in.nextInt();
String top[]=new String[CellNum];
for (int i = 0; i <Rownum+1 ; i++) {
Row row = sheet.createRow(i);//创建第(i+1)行
if(i==0){
for (int j = 0; j < CellNum; j++) {
System.out.println("请输入第"+(j+1)+"个表头名称");
Cell cell=row.createCell(j);//创建第(j+1)个单元格
top[j]=in.next();
cell.setCellValue(top[j]);
}
}
else{
for (int j = 0; j < CellNum; j++) {
System.out.println("请输入第"+i+"个"+top[j]+"的属性值");
Cell cell=row.createCell(j);
cell.setCellValue(in.next());
}
}
}
File file=new File("TestExcel.xlsx");
try {
file.createNewFile();
FileOutputStream stream=new FileOutputStream(file);
book.write(stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}