package com.cmh.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Workbook;
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.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class POIExcelHelper {
/**
* 设置表头样式
* @param workbook
* @return
*/
public static HSSFCellStyle
getTitleStyle(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLUE.index);
font.setFontHeight((
short)
200);
font.setFontName(
"楷体_GB2312");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);
style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 设置表格特别数据样式
* @param workbook
* @return
*/
public static HSSFCellStyle
getDataStyle2(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeight((
short)
200);
font.setFontName(
"楷体_GB2312");
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style.setFont(font);
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 创建单元格内容
* @param row
* @param id
* @param value
* @param style
*/
public static void createCell(HSSFRow row,
int id, String value, HSSFCellStyle style) {
HSSFCell cell = row.createCell(id);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(value);
if (style !=
null) {
cell.setCellStyle(style);
}
}
/**
* 创建报表文件
* @param workbook
* @param dir
* @param filename
* @throws IOException
*/
public static void createFile(HSSFWorkbook workbook, String dir, String filename)
throws IOException {
dir = dir ==
null ?
"" : dir.trim();
if( !
"".equals(dir) ){
if( !dir.endsWith(File.separator) ){
dir += File.separator ;
}
}
File outdir =
new File(dir);
if (!outdir.exists()) {
outdir.mkdirs();
}
FileOutputStream fOut =
new FileOutputStream(dir + filename);
workbook.write(fOut);
fOut.flush();
fOut.close();
}
/**
* 读取Excel中所有的列
* @param filename
* @return
* @throws IOException
*/
private static List<Cell[]>
jxlGetExcelColumns(String filename)
throws IOException {
InputStream is =
null;
jxl.Workbook rwb =
null;
List<Cell[]> list =
new ArrayList<Cell[]>();
try {
is =
new FileInputStream(filename);
rwb = Workbook.getWorkbook(is);
jxl.Sheet rs = rwb.getSheet(
0);
for(
int i=
0; i<rs.getColumns(); i++ ){
list.add(rs.getColumn(i));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
rwb.close();
is.close();
}
return list;
}
/**
* 读取Excel中所有的行
* @param filename
* @return
*/
private static List<Cell[]>
jxlGetExcelRows(String filename) {
InputStream is =
null;
jxl.Workbook rwb =
null;
List<Cell[]> list =
new ArrayList<Cell[]>();
try {
is =
new FileInputStream(filename);
rwb = Workbook.getWorkbook(is);
jxl.Sheet rs = rwb.getSheet(
0);
for(
int i=
0; i<rs.getRows(); i++ ){
list.add(rs.getRow(i));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
rwb.close();
try {
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
测试类
package
com.cmh.test
import java
.io.IOException
import org
.apache.poi.hssf.usermodel.HSSFCellStyle
import org
.apache.poi.hssf.usermodel.HSSFRow
import org
.apache.poi.hssf.usermodel.HSSFSheet
import org
.apache.poi.hssf.usermodel.HSSFWorkbook
import
com.cmh.util.POIExcelHelper
public class ExcelTest {
public static void main(String[] args) {
String title[][] = {
{
"title1",
"title2",
"title3"},
{
"1",
"2",
"3"},
{
"2",
"4",
"6"}
}
// TODO Auto-generated method stub
try {
HSSFWorkbook wb = new HSSFWorkbook()
HSSFSheet sheet = wb
.createSheet()
HSSFCellStyle style = POIExcelHelper
.getTitleStyle(wb)
HSSFRow row = null
for(int i =
0
row = sheet
.createRow(i)
for(int j=
0
POIExcelHelper
.createCell(row,j,title[i][j],style)
}
}
POIExcelHelper
.createFile(wb,
"D://",
"test.xls")
} catch (IOException e) {
// TODO Auto-generated catch block
e
.printStackTrace()
}
}
}
所需的包类
这里写链接内容到这里下载
还有另一个工具类 这里写链接内容
转载请注明原文地址: https://ju.6miu.com/read-19499.html