package test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
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;
import com.sun.java_cup.internal.internal_error;
public class ExportExcel {
public static void main(String[] args) {
HSSFWorkbook bookWorkbook = new HSSFWorkbook();// 创建excel文件
HSSFSheet sheet = bookWorkbook.createSheet("第一个");
HSSFCell cell;
HSSFRow row;
HSSFCellStyle style = bookWorkbook.createCellStyle();// 创建一个单元的样式
// 背景色的设定
style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
// 前景色的设定
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
// 填充模式
style.setFillPattern(HSSFCellStyle.FINE_DOTS);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置水平居中
style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);// 上下居中
// 设置上下左右边框样式
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 设置字体
HSSFFont font = bookWorkbook.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 22);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
String[] headRow1 = { "序号", "学校","", "班级信息","","个人信息","","", "综合考评" };
String[] headRow2 = { "", "名称", "位置", "年级", "几班", "姓名", "性别", "联系方式",
"" };
int rowint = 0;
int titlerow1 = rowint++;
row = sheet.createRow((short) titlerow1);
for (int i = 0; i < headRow1.length; i++) {
cell = row.createCell((short) i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置值的编码格式,确保不会出现乱码
cell.setCellValue(headRow1[i]);
cell.setCellStyle(style);
}
int titlerow2 = rowint++;
row = sheet.createRow(titlerow2);
for (int i = 0; i < headRow2.length; i++) {
cell = row.createCell((short) i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(headRow2[i]);
cell.setCellStyle(style);
}
// 合并单元格
sheet.addMergedRegion(new Region(titlerow1, (short) 1, titlerow1,
(short) 2));// 合并第一行
// 2,三两格,从0开始
sheet.addMergedRegion(new Region(titlerow1, (short) 3, titlerow1,
(short) 4));
sheet.addMergedRegion(new Region(titlerow1, (short) 5, titlerow1,
(short) 7));
sheet.addMergedRegion(new Region(titlerow1, (short) 8, titlerow2,
(short) 8));// 合并行
// 设置每列的宽度
int k = 0;
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
sheet.setColumnWidth((short) k++, (short) 4200);
//放入信息,一般为list,这里用模拟信息
String message[]={"1","清华","北京","计算机","1","snow","女","562771681","有"};
List list=new ArrayList();
list.add(message);
titlerow2++;
for (int i = 0; i < list.size(); i++) {
String mess[]=(String [])list.get(i);
row=sheet.createRow((short)(titlerow2+i));
int kk =0;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[0]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[1]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[2]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[3]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[4]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[5]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[6]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[7]));
kk++;
cell=row.createCell((short)kk);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((mess[8]));
}
try {
FileOutputStream outputStream;
/** 输出信息,导出excel
*
* response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Type", "application/octet-stream");
* OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
*/
try {
outputStream = new FileOutputStream("e://abc.xls");
bookWorkbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
System.err.println("获取不到位置");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
}
}
}