📄 newsputoutexcel.java
字号:
package cn.com.studentsystem.excel;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class NewsPutOutExcel {
public NewsPutOutExcel(){
}
public static WritableWorkbook buildWorkBook(String filename){
File file = new File(filename);
WritableWorkbook excel = null;
try {
excel = Workbook.createWorkbook(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return excel;
}
public static WritableSheet setExcel(WritableWorkbook excel,int row,Vector vector){
// 设置列表头信息
WritableSheet sheet = excel.createSheet("中信软件29班消息表", 1);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 20,
WritableFont.NO_BOLD);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
WritableFont headFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD);
WritableCellFormat headFormat = new WritableCellFormat(headFont);
try {
sheet.mergeCells(0, 0, 9, 1);
titleFormat.setAlignment(Alignment.CENTRE);
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
Label title_name = new Label(0, 0, "中信软件29班消息表"// 设置的标题
, titleFormat);
sheet.addCell(title_name);
headFormat.setAlignment(Alignment.CENTRE);
headFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
headFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
sheet.mergeCells(0, 2, 1, 2);
Label dateCol = new Label(0, 2, "时间日期", headFormat);
sheet.addCell(dateCol);
sheet.mergeCells(2, 2, 7, 2);
Label titleCol = new Label(2, 2, "内容标题", headFormat);
sheet.addCell(titleCol);
sheet.mergeCells(8, 2, 9, 2);
Label authorCol = new Label(8, 2, "消息发布人", headFormat);
sheet.addCell(authorCol);
for(int i=3;i<row+3;i++){
sheet.mergeCells(0, i, 1, i);
sheet.mergeCells(2, i, 7, i);
sheet.mergeCells(8, i, 9, i);
}
for(int j=0;j<row;j++){
// for(int k=0;k<2;k++){
//
// sheet.addCell(new Label(k,j+3,vector.elementAt(j*k).toString(),headFormat));
//
// }
sheet.addCell(new Label(0,j+3,vector.elementAt(j*3).toString(),headFormat));
sheet.addCell(new Label(2,j+3,vector.elementAt(j*3+1).toString(),headFormat));
sheet.addCell(new Label(8,j+3,vector.elementAt(j*3+2).toString(),headFormat));
}
excel.write();
excel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
return sheet;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -