📄 exceltableexporter.java
字号:
package com.cownew.PIS.ui.ctrl.table;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import javax.swing.JTable;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.cownew.ctk.io.ResourceUtils;
/**
* 将JTable的数据导出到excel
*
* @author 杨中科
*
*/
public class ExcelTableExporter
{
private JTable table;
/**
* 范围的空值表示,如果为此值,则表示被指定范围为默认
*/
public final static short DEFAULTVALUE = -1;
private short left;
private short right;
private int top;
private int bottom;
public ExcelTableExporter(JTable table)
{
super();
this.table = table;
left = DEFAULTVALUE;
right = DEFAULTVALUE;
top = DEFAULTVALUE;
bottom = DEFAULTVALUE;
}
/**
* 设定要导出表格的范围
*
* @param left
* 左边界,如果为DEFAULTVALUE则表示从0开始
* @param right
* 右边界,如果为DEFAULTVALUE则表示直到最右的位置
* @param top
* 上边界,如果为DEFAULTVALUE则表示从0开始
* @param bottom
* 下边界,如果为DEFAULTVALUE则表示直到最下的位置
*/
public void setBounds(short left, short right, int top, int bottom)
{
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
}
/**
* 导出到文件
*
* @param file
* @throws IOException
*/
public void export(File file) throws IOException
{
FileOutputStream foStream = null;
try
{
foStream = new FileOutputStream(file);
export(foStream);
} finally
{
ResourceUtils.close(foStream);
}
}
/**
* 导出到流
*
* @param outStream
* @throws IOException
*/
public void export(OutputStream outStream) throws IOException
{
int rowCount = table.getRowCount();
int colCount = table.getColumnCount();
short leftB = (left == DEFAULTVALUE) ? 0 : left;
short rightB = (short) ((right == DEFAULTVALUE) ? colCount - 1 : right);
int topB = (top == DEFAULTVALUE) ? 0 : top;
int bottomB = (bottom == DEFAULTVALUE) ? rowCount - 1 : bottom;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
for (int rowIndex = topB; rowIndex <= bottomB; rowIndex++)
{
HSSFRow row = sheet.createRow(rowIndex - topB);
for (short colIndex = leftB; colIndex <= rightB; colIndex++)
{
HSSFCell cell = row.createCell((short) (colIndex - leftB));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
Object value = table.getValueAt(rowIndex, colIndex);
setCellValue(workbook, cell, value);
}
}
workbook.write(outStream);
}
private static void setCellValue(HSSFWorkbook workbook, HSSFCell cell,
Object value)
{
if (value == null)
{
cell.setCellValue("");
} else if (value instanceof Date)
{
cell.setCellValue((Date)value);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat
.getBuiltinFormat("m/d/yy h:mm"));
cell.setCellStyle(cellStyle);
} else if (value instanceof Boolean)
{
Boolean b = (Boolean) value;
cell.setCellValue(b.booleanValue());
} else if (value instanceof Number)
{
Number num = (Number) value;
cell.setCellValue(num.doubleValue());
} else
{
cell.setCellValue(value.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -