「java头表与行表」java设置表头

博主:adminadmin 2022-12-17 18:45:06 67

本篇文章给大家谈谈java头表与行表,以及java设置表头对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java表与表之间关联怎么整

如果你说的是数据库中表与表之间的联系的话;首先你是在表对应的bean.hbm.xml中配置各个表之间的关系。 各种set 或many-to-many往上扔,找本书参考最好了,这样解决最快,在论坛上不够系统,希望楼主能解决问题

java 导出excel 怎么设置 表头跨行 跨列

导Excel满足条件:1)纯文本或都考虑格式;2)文件体积或数据量太考虑导Excel文件另存.csv格式用Word打Ctrl+H需要要文字替换换行符(^p)存盘再用Excel打能换行结Excel文件文本格式单元格替换候能要考虑双引号

如何在java中做excel表

很简单,这有代码:代码里面数据库访问对象自己写,用到第三方架包:

poi-2.5-final-20040302.jar;

poi-contrib-2.5-final-20040302.jar

poi-scratchpad-2.5-final-20040302.jar

自己去网上下吧

//处理类

public class CountExcel {

/**

* 数据库操作类,自己写吧。。。

*/

private OperData jdbc /*自己写*/;

/**

* 创建excel,返回excel的存放地址

*

* @param title

* 标题

* @param sql

* 查询语句

* @param path

* excel的存放路径(物理地址)

* @param titlename

* 报表的名字

* @return 路径

*/

public String createExcelForAssess(String[] title, String sql, String path,

String titlename) throws Exception {

GregorianCalendar c = new GregorianCalendar();

StringBuffer excelUrl = new StringBuffer();

java.util.Date now = c.getTime();

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm");

String strNow = format.format(now);

excelUrl.append(path).append(File.separator).append("te_").append(

strNow).append(".xls");

// excel的表头

Vector vcCol = new Vector();

if (title == null || title.length 1)

return "";

for (int i = 0; i title.length; i++) {

vcCol.add(title[i]);

}

int[] align = new int[vcCol.size()];

int[] num = new int[vcCol.size()];

for (int i = 0; i vcCol.size(); i++) {

align[i] = 2;

num[i] = 0;

}

Vector vc = getqueryrest(sql);

ExcelReport excel = new ExcelReport();

excel.setExcelFile(excelUrl.toString());

excel.setCols(vcCol.size());

excel.createCaption(titlename);

excel.createColumnCaption(vcCol);

excel.createBody(vc, align, num);

excel.createPage(5);

excel.createFile();

return excelUrl.toString();

}

/**

* 查询结果集

*

* @param sql传入查询的sql语句

* @return Vector

* @throws SQLException

*/

public Vector getqueryrest(String sql) throws SQLException {

Vector vc = new Vector();

//数据库查询,返回的list里面存的是数据的pojo对象

//List list = jdbc.getQueryResult(sql);

if (list != null list.size() 0) {

for (int i = 0; i list.size(); i++) {

String[] info = (String[]) list.get(i);

for (int j = 0; j info.length; j++) {

vc.add(info[j]);

}

}

}

return vc;

}

}

//工具类

public class ExcelReport{

/**

* EXCEL文件工作区

*/

private HSSFWorkbook wb;

/**

* EXCEL文件SHEET

*/

private HSSFSheet sheet;

/**

* EXCEL文件存放的目录路径

*/

private String excelFile = "";

/**

* 记录当前行数

*/

private int rownum = 0;

/**

* 记录总共列数

*/

private int cols = 0;

/**

* 记录每列的宽度

*/

private int[] length;

/**

* 记录是否已经设定字段数

*/

private boolean flag = false;

/**

* 设置EXCEL文件存放的目录路径,在生成标题,列头前设定

*/

public void setExcelFile(String excelFile){

this.excelFile = excelFile;

}

/**

* 设置EXCEL表的列数,在生成标题,列头前设定

*/

public void setCols(int cols){

this.cols = cols;

if(flag){

if(length.length cols){

length = getLength(length);

}

}else{

length = new int[cols];

}

flag = true;

}

/**

* 第二次设定字段数,保存第一张表格每列的长度

*/

private int[] getLength(int[] arr){

int[] temp = new int[cols];

for(int i=0;iarr.length;i++){

temp[i] = arr[i];

}

return temp;

}

/**

* 初始化EXCEL报表类

*/

public ExcelReport(){

wb = new HSSFWorkbook();

sheet = wb.createSheet("new sheet");

}

/**

* 生成标题

* @param caption 标题头

*/

public void createCaption(String caption){

//生成标题行

HSSFRow row = sheet.createRow((short)rownum);

//生成标题单元格

HSSFCell cell = row.createCell((short)0);

//生成标题单元格样式

HSSFCellStyle style = wb.createCellStyle();

//设定标题字体

HSSFFont font = wb.createFont();

font.setFontHeightInPoints((short)14);

font.setFontName("新宋体");

font.setColor(HSSFColor.BLACK.index);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

style.setFont(font);

//设定标题框格式

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setLeftBorderColor(HSSFColor.BLACK.index);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setRightBorderColor(HSSFColor.BLACK.index);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

//设置cell编码解决中文高位字节截断

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设定单元格文字合样式

cell.setCellValue(caption);

cell.setCellStyle(style);

for(int i=1;icols;i++){

cell = row.createCell((short)i);

cell.setCellStyle(style);

cell.setCellValue("");

}

//设定合并的单元格

sheet.addMergedRegion(new Region(rownum,(short)0,rownum,(short)(cols-1)));

//当前行自增

rownum++;

}

/**

* 生成列头

* @param vc 列头内容

*/

public void createColumnCaption(Vector vc){

//生成列头行

HSSFRow row = sheet.createRow((short)rownum);

//生成了列头格式

HSSFCellStyle style = wb.createCellStyle();

//设定列头字体

HSSFFont font = wb.createFont();

font.setFontHeightInPoints((short)10);

font.setFontName("新宋体");

font.setColor(HSSFColor.BLACK.index);

style.setFont(font);

//设定标题框格式

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setLeftBorderColor(HSSFColor.BLACK.index);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setRightBorderColor(HSSFColor.BLACK.index);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

//生成列头单元格

HSSFCell cell;

for(int i=0;ivc.size();i++){

//生成标题单元格

cell = row.createCell((short)i);

//设置cell编码解决中文高位字节截断

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设定单元格文字合样式

if(vc.get(i) != null){

cell.setCellValue(vc.get(i).toString());

//记录列头的长度

if(vc.get(i).toString().getBytes().length length[i]){

length[i] = vc.get(i).toString().getBytes().length;

}

}else{

cell.setCellValue("");

}

cell.setCellStyle(style);

}

rownum++;

}

/**

* 生成合并的列头

* @param vc 列头

* @param colnum 每列要合并的列数,并且所有要合并的列数之和等于总表格列数

*/

public void mergeColumnCaption(Vector vc,int[] colnum){

//生成标题行

HSSFRow row = sheet.createRow((short)rownum);

//生成标题单元格样式

HSSFCellStyle style = wb.createCellStyle();

//设定标题字体

HSSFFont font = wb.createFont();

font.setFontHeightInPoints((short)10);

font.setFontName("新宋体");

font.setColor(HSSFColor.BLACK.index);

style.setFont(font);

//设定标题框格式

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setLeftBorderColor(HSSFColor.BLACK.index);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setRightBorderColor(HSSFColor.BLACK.index);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

int pos = 0;

HSSFCell cell;

for(int i=0;ivc.size();i++){

pos = pos + colnum[i];

cell = row.createCell((short)(pos-colnum[i]));

//设置cell编码解决中文高位字节截断

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设定单元格文字合样式

cell.setCellStyle(style);

if(vc.get(i) == null){

cell.setCellValue("");

}else{

cell.setCellValue(vc.get(i).toString());

}

for(int j=1;jcolnum[i];j++){

cell = row.createCell((short)(pos-colnum[i]+j));

cell.setCellStyle(style);

cell.setCellValue("");

}

//设定合并的单元格

sheet.addMergedRegion(new Region(rownum,(short)(pos-colnum[i]),rownum,(short)(pos-1)));

}

//当前行自增

rownum++;

}

/**

* 合并行

* @param startrow 起始行

* @param endrow 终止行

* @param column 列数

*/

public void mergeRowCaption(int startrow,int endrow,int column){

sheet.addMergedRegion(new Region(startrow,(short)column,endrow,(short)column));

}

/**

* 生成表格主体

* @param vc 表格内容

* @param align 每列的对齐方式,1为左,2为中,3为右,数组长度要等于表格列数

* @param num 每列的数据类型,0为字符串,1为数字

*/

public void createBody(Vector vc,int[] align,int[] num){

int rows = vc.size() / cols;

//设定表格字体

HSSFFont font = wb.createFont();

font.setFontHeightInPoints((short)10);

font.setFontName("新宋体");

font.setColor(HSSFColor.BLACK.index);

font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

HSSFRow row;

HSSFCell cell;

//设定数据格式

HSSFDataFormat df = wb.createDataFormat();

//生成了左对齐表格格式

HSSFCellStyle styleLeft = wb.createCellStyle();

styleLeft.setFont(font);

styleLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);

styleLeft.setBottomBorderColor(HSSFColor.BLACK.index);

styleLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);

styleLeft.setLeftBorderColor(HSSFColor.BLACK.index);

styleLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);

styleLeft.setRightBorderColor(HSSFColor.BLACK.index);

styleLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);

styleLeft.setTopBorderColor(HSSFColor.BLACK.index);

styleLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

styleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);

styleLeft.setDataFormat(df.getFormat("##################.00"));

//生成居中对齐表格格式

HSSFCellStyle styleCenter = wb.createCellStyle();

styleCenter.setFont(font);

styleCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);

styleCenter.setBottomBorderColor(HSSFColor.BLACK.index);

styleCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);

styleCenter.setLeftBorderColor(HSSFColor.BLACK.index);

styleCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);

styleCenter.setRightBorderColor(HSSFColor.BLACK.index);

styleCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);

styleCenter.setTopBorderColor(HSSFColor.BLACK.index);

styleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);

styleCenter.setDataFormat(df.getFormat("##################.00"));

//生成右对齐表格格式

HSSFCellStyle styleRight = wb.createCellStyle();

styleRight.setFont(font);

styleRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);

styleRight.setBottomBorderColor(HSSFColor.BLACK.index);

styleRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);

styleRight.setLeftBorderColor(HSSFColor.BLACK.index);

styleRight.setBorderRight(HSSFCellStyle.BORDER_THIN);

styleRight.setRightBorderColor(HSSFColor.BLACK.index);

styleRight.setBorderTop(HSSFCellStyle.BORDER_THIN);

styleRight.setTopBorderColor(HSSFColor.BLACK.index);

styleRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

styleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

styleRight.setDataFormat(df.getFormat("##################.00"));

for (int i = 0;i rows; i++){

// 创建新行

row = sheet.createRow((short)(rownum));

for (int j = 0;j cols; j++){

// 创建一个单元格

cell = row.createCell((short)j);

//设置cell编码解决中文高位字节截断

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设置cell字符类型的值

if(vc.get(i*cols+j) == null){

cell.setCellValue("");

}else{

if(num[j] == 0){

cell.setCellValue(vc.get(i*cols+j).toString());

}else{

cell.setCellValue(Double.parseDouble(vc.get(i*cols+j).toString()));

}

//记录每列的长度

if(vc.get(i*cols+j).toString().getBytes().length length[j]){

length[j] = vc.get(i*cols+j).toString().getBytes().length;

}

}

//设定对齐方式

if(align[j] == 1){

cell.setCellStyle(styleLeft);

}

if(align[j] == 2){

cell.setCellStyle(styleCenter);

}

if(align[j] == 3){

cell.setCellStyle(styleRight);

}

}

rownum++;

}

}

/**

* 生成统计结果行

* @param stat 统计结果

* @param align 对齐方式,1为左,2为中,3为右

*/

public void createStat(String stat,int align){

//生成统计结果行

HSSFRow row = sheet.createRow((short)rownum);

//生成统计结果格

HSSFCell cell = row.createCell((short)0);

//生成统计结果单元格样式

HSSFCellStyle style = wb.createCellStyle();

//设定统计结果字体

HSSFFont font = wb.createFont();

font.setFontHeightInPoints((short)10);

font.setFontName("新宋体");

font.setColor(HSSFColor.BLACK.index);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

style.setFont(font);

//设定标题框格式

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setLeftBorderColor(HSSFColor.BLACK.index);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setRightBorderColor(HSSFColor.BLACK.index);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式

if(align == 1){

style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

}

if(align == 2){

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

}

if(align == 3){

style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

}

//设置cell编码解决中文高位字节截断

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设定单元格文字合样式

cell.setCellValue(stat);

cell.setCellStyle(style);

for(int i=1;icols;i++){

cell = row.createCell((short)i);

cell.setCellStyle(style);

cell.setCellValue("");

}

//设定合并的单元格

sheet.addMergedRegion(new Region(rownum,(short)0,rownum,(short)(cols-1)));

//当前行自增

rownum++;

}

/**

* 设置页眉

* @param header 页眉内容

* @param align 对齐方式,1为左,2为中,3为右

*/

public void createHeader(String header,int align){

HSSFHeader head = sheet.getHeader();

if(align == 1){

head.setLeft(header);

}

if(align == 2){

head.setCenter(header);

}

if(align == 3){

head.setRight(header);

}

}

/**

* 设置页脚

* @param footer 页脚内容

* @param align 对齐方式,1为左,2为中,3为右

*/

public void createFooter(String footer,int align){

HSSFFooter foot = sheet.getFooter();

if(align == 1){

foot.setLeft(footer);

}

if(align == 2){

foot.setCenter(footer);

}

if(align == 3){

foot.setRight(footer);

}

}

/**

* 设定页脚的页面值

* @param align 对齐方式,1为左,2为中,3为右

*/

public void createPage(int align){

HSSFFooter foot = sheet.getFooter();

if(align == 1){

foot.setLeft("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());

}

if(align == 2){

foot.setCenter("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());

}

if(align == 3){

foot.setRight("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());

}

}

/**

* 生成EXCEL文件

*/

public void createFile() throws Exception{

for(int i=0;ilength.length;i++){

sheet.setColumnWidth((short)i,(short)(length[i]*2*200));

}

FileOutputStream fileOut = new FileOutputStream(excelFile);

wb.write(fileOut);

fileOut.close();

}

}

关于java头表与行表和java设置表头的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-17,除非注明,否则均为首码项目网原创文章,转载请注明出处。