/*
- Copyright 2002-2019 Petro-CyberWorks Information Technology Co. Ltd. All rights reserved.
- PCITC PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.pcitc.scl.business.productoil.util.excel;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;
/**
-
@description
-
@author sufangfang
-
@version 1.0, 2019年2月22日
*/
public class Test {public static void main(String[] args) {
File newFile = ExcelUtil.createNewFile(“经营管理-销售管理-出口销售.xlsx”);// 新文件写入数据,并下载***************************************************** InputStream is = null; XSSFWorkbook workbook = null; XSSFSheet sheet = null; try { is = new FileInputStream(newFile);// 将excel文件转为输入流 workbook = new XSSFWorkbook(is);// 创建个workbook, // 获取第一个sheet sheet = workbook.getSheetAt(0); } catch (Exception e1) { e1.printStackTrace(); } if (sheet != null) { try { // 写数据 FileOutputStream fos = new FileOutputStream(newFile); XSSFRow row = sheet.getRow(0); if (row == null) { row = sheet.createRow(0); } else { Iterator<Cell> it = row.cellIterator(); while (it.hasNext()) { Cell cell = it.next(); cell.setCellStyle(buildHeaderStyle(workbook)); } } XSSFCell cell = row.getCell(0); if (cell == null) { cell = row.createCell(0); } workbook.write(fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != is) { is.close(); } } catch (Exception e) { e.printStackTrace(); } } }
}
public static XSSFCellStyle buildHeaderStyle(XSSFWorkbook workbook) {
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); //横向居中
style.setVerticalAlignment(VerticalAlignment.CENTER); //竖向居中
//设置边框
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
//字体样式
XSSFFont font = workbook.createFont();
font.setBold(true); //加粗
font.setColor(Font.COLOR_NORMAL);//黑色
font.setFontName(“黑体”);
font.setFontHeight(12); //字体大小
style.setFont(font);
return style;
}
}