📄 differetstudenttable.java
字号:
package cn.com.WriterDataToExcelFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
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 DifferetStudentTable {
public static void main(String[] args) {
}
/**
* 创建excel文件
*
* @param fileName
* @param vectorData
* @param vectorTitle
*/
public void writeExcelOfFile(String fileName, Vector vectorData,
Vector vectorTitle) {
WritableWorkbook excelModel = null; // Excle文件写出操作对象
try {
// 创建Excel文件写出操作对象
File file = new File(fileName);
excelModel = Workbook.createWorkbook(file);
// 调用生成Excel列表方法设置并生成Excel
this.writeExcelFile(excelModel, vectorData);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
excelModel.close(); // 必须关闭创建Excel文档的对象
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 写出Excel文件
*
* @param excelModel
* @param argVectorData
* 要生成excel文件的数据
* @throws WriteOutExcelException
*/
private void writeExcelFile(WritableWorkbook excelModel,
Vector argVectorData) throws Exception {
try {
/* 设置标题 */
String title = "中心软件教育中心29班学生成绩表";
// 设置页面Sheet
WritableSheet ws = excelModel.createSheet(title, 2);
// 设置头
this.buildExlHead(ws, title);
// 每一个单元格的样式
WritableCellFormat cellFormat = new WritableCellFormat();
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
Iterator columnIterator = argVectorData.iterator(); // 列的叠带器
Vector rowVector = null; // 行的向量对象
int rowNum = 6; // 值起始行号
for (; columnIterator.hasNext();) {
rowVector = (Vector) columnIterator.next();
for (int i = 0, n = rowVector.size(); i < n; i++) {
ws.addCell(new Label(i, rowNum, rowVector.elementAt(i)
.toString(), cellFormat));
}
rowNum++;
}
// 写出Excel文件
excelModel.write(); // 必须调用写文件的方法
} catch (FileNotFoundException fne) {
fne.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (WriteException wex) {
wex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 设置Excel报表标题
*
* @param sheet
* Excel操作对象的Sheet
* @param argTitle
* 标题名称
* @param colunmUnite
* 合并列数
* @param rowUnite
* 合并行数
* @return WritableSheet
* @throws WriteException
*/
protected WritableSheet setExcelTitle(WritableSheet sheet, String argTitle,
int colunmUnite, int rowUnite) throws WriteException {
// 设置合并标题栏的格式
sheet.mergeCells(0, 0, colunmUnite, rowUnite);
// 定义标题字体等信息
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 20,
WritableFont.NO_BOLD);
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
// 设置表头居中显示
titleFormat.setAlignment(Alignment.CENTRE);
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
Label title = new Label(0, 0, argTitle// 设置的标题
, titleFormat);
sheet.addCell(title);
return sheet;
}
/**
* 生成销售对帐单Excel报表的报表头部
*
* @param sheet
* WritableSheet
* @return WritableSheet
* @throws WriteException
*/
public WritableSheet buildExlHead(WritableSheet sheet, String argTitle)
throws Exception {
try {
// 创建标题
setExcelTitle(sheet, argTitle, 25, 0);
// 设置列表头信息
WritableFont headFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD);
WritableCellFormat headFormat = new WritableCellFormat(headFont);
headFormat.setAlignment(Alignment.CENTRE);
headFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
headFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置院系列的样式
sheet.mergeCells(0, 1, 0, 5); // 0 列的起始数 1 行的起始数
Label dateCol = new Label(0, 1, "姓名", headFormat);
sheet.addCell(dateCol);
int k = TableFrame.jTable2.getColumnCount();
System.out.println(TableFrame.jTable2.getColumnName(1));
String[] str = new String[k];
for (int j = 0; j < k; j++) {
str[j] = TableFrame.jTable2.getColumnName(j);
}
Label[] column = new Label[str.length];
for (int i = 0; i < str.length; i++) {
sheet.mergeCells(1, 1, 5, 1);
column[i] = new Label(1, 1, str[i], headFormat);
sheet.addCell(column[i]);
}
return sheet;
} catch (WriteException ex) {
ex.printStackTrace();
}
return sheet;
}
public static Vector getVectorData() {
Vector veData = new Vector();
/** 九九级 */
Vector vd = new Vector();
int i = TableFrame.jTable2.getRowCount();
int k = TableFrame.jTable2.getColumnCount();
String[] str = null;
for(int j = 0;j<i;j++){
for(int g=0;g<k;g++){
vd.add(TableFrame.jTable2.getValueAt(j, g));
}
}
veData.add(vd);
return veData;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -