poi table export data to an Excel file

poi table export data to an Excel file

   点关注不迷路,欢迎再访!		

Learning to article: https: //blog.csdn.net/qq_42944520/article/details/84591565

Database table data:

Here Insert Picture Description

The introduction of dependence

<!-- 添加poi 依赖 -->
   <dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi</artifactId>
		<version>3.17</version>
  </dependency>
  <dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi-ooxml</artifactId>
		<version>3.17</version>
  </dependency>

Tools

@Service("userService")
public class UserServiceImpl implements IUserService {
	
	private Logger logger = LogManager.getLogger(UserServiceImpl.class.getName());	

	@Autowired
	private UserMapper userMapper;

	public String findAllExcel() {
		List<User> lists = userMapper.findAll();
		// poi的导出到表格
		// 创建文档对象
		HSSFWorkbook wb = new HSSFWorkbook();
		// 获取style对象
		HSSFCellStyle st = wb.createCellStyle();
		// 设置一个时间的格式,时间格式需要特殊处理
		st.setDataFormat(wb.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
		// 获取表单对象
		HSSFSheet sheet = wb.createSheet();
		// 创建行
		HSSFRow row = sheet.createRow(0);
		// 首先将第一行数据写出来
		// 定义一个数组
		String arr[] = { "编号", "用户名", "密码", "创建人", "创建时间", "修改人", "修改书时间", "是否有效" };
		for (int i = 0; i < arr.length; i++) {
			// 给第一行插入数据
			row.createCell(i).setCellValue(arr[i]);
		}
		// 写list集合中的数据,从第一行开始写数据
		for (int i = 1; i < lists.size() + 1; i++) {
			// 创建行
			HSSFRow row2 = sheet.createRow(i);
			// 在创建行的基础上创建列,并写入数据
			row2.createCell(0).setCellValue(lists.get(i - 1).getId());
			row2.createCell(1).setCellValue(lists.get(i - 1).getUsername());
			row2.createCell(2).setCellValue(lists.get(i - 1).getPassword());
			row2.createCell(3).setCellValue(lists.get(i - 1).getCreatedBy());
			row2.createCell(5).setCellValue(lists.get(i - 1).getUpdateBy());
			row2.createCell(7).setCellValue(lists.get(i - 1).getIsVaild());
			// 设置日期格式
			HSSFCell cell = row2.createCell(4);
			cell.setCellStyle(st);
			cell.setCellValue(lists.get(i - 1).getCreatedDate());
			HSSFCell cell1 = row2.createCell(6);
			cell1.setCellStyle(st);
			cell1.setCellValue(lists.get(i - 1).getUpdateDate());

		}
		// 获取上传路径
		String path = "C:\\Users\\64966\\\\Desktop";
		System.out.println(path);
		// 获取file对象
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		// 获取输出流对象
		try {
			FileOutputStream out = new FileOutputStream(path + "/用户表.xls");
			wb.write(out);
			logger.info("数据导出成功!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return path + "/用户表.xls";
	}

}

Test categories: ---- effective pro-test

/**
 * 用户测试类
 * @author andy
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
	
	@Autowired
	private IUserService userService;
	
	@Test
	public void excelTest() {
		String flag= userService.findAllExcel();
		System.out.println(flag);
	}

}

The results shown in Figure:
Here Insert Picture Description
The share ended!

Published 101 original articles · won praise 33 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_39443053/article/details/103705172