package common.excelOprate;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
public class WriteExcel {
public WriteExcel() {
super();
// TODO 自动生成构造函数存根
}
/**
* @param args
* @throws IOException
*/
public String createExcel(){
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet();
// 为表格的列创建显示类型
HSSFCellStyle columnHeaderStyle = workBook.createCellStyle();
columnHeaderStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
columnHeaderStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index);
// 创建字体
HSSFFont font = workBook.createFont();
font.setColor(HSSFFont.COLOR_RED);
columnHeaderStyle.setFont(font);
// 设定表格每一列的显示风格
//headerCell1.setCellStyle(columnHeaderStyle);
//headerCell2.setCellStyle(columnHeaderStyle);
//创建表格行的数据内容
HSSFRow row = sheet.createRow((short) 0);
HSSFCell c11=row.createCell((short) 0);
c11.setCellValue("订单编号");
c11=row.createCell((short) 1);
c11.setCellValue("集团名称");
c11=row.createCell((short) 2);
c11.setCellValue("内容");
c11=row.createCell((short) 3);
c11.setCellValue("提交日期");
c11=row.createCell((short) 4);
c11.setCellValue("完成日期");
c11=row.createCell((short) 5);
c11.setCellValue("区域");
for(int i=1;i<10;i++){
row = sheet.createRow((short) i);
c11=row.createCell((short) 0);
c11.setCellValue(i+"0");
c11=row.createCell((short) 1);
c11.setCellValue(i+"0");
c11=row.createCell((short) 2);
c11.setCellValue(i+"0");
c11=row.createCell((short) 3);
c11.setCellValue(i+"0");
c11=row.createCell((short) 4);
c11.setCellValue(i+"0");
c11=row.createCell((short) 5);
c11.setCellValue(i+"0");
}
//写文件,完成保存任务
Date currTime = new Date();
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMddhhmmss", Locale.US);
String fileName =formatter2.format(currTime)+ ".xls";
FileOutputStream stream = null;
try {
stream = new FileOutputStream("excel/"+fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
workBook.write(stream);
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
public static void main(String[] args) throws SQLException, Exception {
WriteExcel createXL=new WriteExcel();
createXL.createExcel();
}
}