poi realizes Excel file writing supports suffix XLSX format

//poi实现Excel文件写入  支持后缀XLSX格式
	public static void writeExcelTwo() throws IOException{
		String[] title = {"username","password","phone"};
		//创建工作簿
		XSSFWorkbook workbook = new XSSFWorkbook();
		//创建工作表
		XSSFSheet sheet = workbook.createSheet();
		//创建表中的第一行  索引为0
		XSSFRow row = sheet.createRow(0);
		//创建单元格
		XSSFCell cell = null;
		//在第一行插入单元格设置值
		for(int i=0;i<title.length;i++){
			cell = row.createCell(i);
			cell.setCellValue(title[i]);
		}
		//在后面追加数据
		//从索引1开始  因为0上是表头名称
		for(int i=1;i<20;i++){
			//创建新的下一行  i为行数
			XSSFRow nextRow = sheet.createRow(i);
			//创建下一行的单元格对象 索引是一行中的第几个单元格
			XSSFCell cell2 = nextRow.createCell(0);
			cell2.setCellValue("名字"+i);
			cell2 = nextRow.createCell(1);
			cell2.setCellValue("密码"+i);
			cell2 = nextRow.createCell(2);
			cell2.setCellValue("电话"+i);
		}
		//创建一个文件
		File file = new File("F:\\html练习文件\\lianxi\\poi_test1.xlsx");
		//将Excel文件写入创建的file当中
		OutputStream stream = new FileOutputStream(file);
		workbook.write(stream);
		//关闭流
		stream.close();
	}

Guess you like

Origin blog.csdn.net/qq_36551486/article/details/82911345