POM简单示例

maven所需价包:


			org.apache.poi
			poi
			3.17
		
		
			org.apache.poi
			poi-ooxml-schemas
			3.17
		
		
			org.apache.poi
			poi-ooxml
			3.17

/**

  • HSSF是POI项目的Excel '97(-2007)文件格式的纯Java实现。
  • XSSF是POI Project的Excel 2007 OOXML(.xlsx)文件格式的纯Java实现
  • 其他的完全一致

*/
public class Upload {
public static void read() {
File file = new File(“D:/1111.xlsx”);
System.out.println(file.getPath());
try {
System.out.println(“1111”);
// 读取excel文件
FileInputStream in = new FileInputStream(“D:\123.xlsx”);
// 将输入流转换为workbook对象
XSSFWorkbook workbook = new XSSFWorkbook(in);
// 获取工作表获取工作表
XSSFSheet sheet = workbook.getSheetAt(0);
// 获取行
XSSFRow row = sheet.getRow(1);
// 读取单元格
XSSFCell cell = row.getCell(1);
// 得到单元格的内容
String value = cell.getStringCellValue();
// 打印到控制台
System.out.println(“B2的单元格内容为:” + value);
} catch (Exception e) {
// TODO: handle exception
}
}

public static void read2() throws IOException {
try {
System.out.println(“1111”);
// 读取excel文件
FileInputStream in = new FileInputStream(“D:\123.xlsx”);
// 将输入流转换为workbook对象
HSSFWorkbook workbook = new HSSFWorkbook(in);
// 获取工作表获取工作表XSS
HSSFSheet sheet = workbook.getSheetAt(0);
// 获取行
HSSFRow row = sheet.getRow(1);
// 读取单元格
HSSFCell cell = row.getCell(1);
// 得到单元格的内容
String value = cell.getStringCellValue();
// 打印到控制台
System.out.println(“B2的单元格内容为:” + value);
} catch (Exception e) {
// TODO: handle exception
}

}

/**

  • 生成表格

*/
public static void write() {
HSSFWorkbook workbook = new HSSFWorkbook();
//样式
HSSFCellStyle setBorder = workbook.createCellStyle();
//字体
HSSFFont font = workbook.createFont();
//创建sheet
HSSFSheet sheet = workbook.createSheet(“文本文档”);
HSSFRow row = sheet.createRow(1);
HSSFCell cell = row.createCell(1);
cell.setCellValue(“hello herman”);
font.setFontName(“仿宋_GB2312”);
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font.setFontHeightInPoints((short) 22);
setBorder.setFillForegroundColor((short) 13);// 设置背景色
//setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
//setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
//setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
//setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
setBorder.setFont(font);//选择需要用到的字体格式
setBorder.setWrapText(true);//设置自动换行
cell.setCellStyle(setBorder);
try {
File file=new File(“D:\123456.xls”);
if(file.exists()) {
file.createNewFile();
}

	FileOutputStream outputStream = new FileOutputStream(file);
	workbook.write(outputStream);
	outputStream.close();
	} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 

}
public static void write2() {
XSSFWorkbook workbook = new XSSFWorkbook();
//样式
XSSFCellStyle setBorder = workbook.createCellStyle();
//字体
XSSFFont font = workbook.createFont();
//创建sheet
XSSFSheet sheet = workbook.createSheet(“文本文档”);
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellValue(“hello herman”);
font.setFontName(“仿宋_GB2312”);
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font.setFontHeightInPoints((short) 22);
setBorder.setFillForegroundColor((short) 13);// 设置背景色
//setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
//setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
//setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
//setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
setBorder.setFont(font);//选择需要用到的字体格式
setBorder.setWrapText(true);//设置自动换行
cell.setCellStyle(setBorder);
try {
FileOutputStream outputStream = new FileOutputStream(“D:\13.xlsx”);
workbook.write(outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

你可能感兴趣的:(java,pom示例)