重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
利用java操作Excel源东东-jxl.jar读取前单元格值 String cellValue = cells[k].getContents(); sb.append(cellValue+" "); } } sb.append("
10年积累的成都网站设计、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有钢城免费网站建设让你可以放心的选择与我们合作。
"); } sb.append("
"); } } //关闭资源释放内存 wb.close(); return sb.toString(); } 二.写入Excel文件格式比文本内容加粗加某些颜色等参考jxlapi同推荐篇错文章:/developerworks/cn/java/l-javaExcel/?ca=j-t10java 代码public static void writeExcel(String fileName){ WritableWorkbook wwb = null; try { //首先要使用Workbook类工厂创建写入工作薄(Workbook)象 wwb = Workbook.createWorkbook(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } if(wwb!=null){ //创建写入工作表 //WorkbookcreateSheet两参数第工作表名称第二工作表工作薄位置 WritableSheet ws = wwb.createSheet("sheet1", 0); //面始添加单元格 for(int i=0;i0){ //每工作表进行循环 for(int i=0;iif(breakSheet) break; //前工作表行数 int rowNum = sheet[i].getRows(); boolean breakRow = false; for(int j=0;jif(breakRow) break; //前行所单元格 Cell[] cells = sheet[i].getRow(j); if(cells!=nullcells.length0){ boolean breakCell = false; //每单元格进行循环 for(int k=0;kif(breakCell) break; //读取前单元格值 String cellValue = cells[k].getContents(); if(cellValue==null) continue; if(cellValue.contains(keyWord)){ res = true; breakCell = true; breakRow = true; breakSheet = true; } } } } } } //关闭资源释放内存 wb.close(); return res; } 四.往Excel插入图片图标插入图片实现容易参看代码:java 代码public static void insertImg(WritableSheet dataSheet, int col, int row, int width, int height, File imgFile){ WritableImage img = new WritableImage(col, row, width, height, imgFile); dataSheet.addImage(img); } 代码注释已经清楚概用再解释我用程序验证:java 代码try { //创建工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls")); //待插入工作表 WritableSheet imgSheet = workbook.createSheet("Images",0); //要插入图片文件 File imgFile = new File("D:/1.png"); //图片插入第二行第单元格宽各占六单元格 insertImg(imgSheet,0,1,6,6,imgFile); workbook.write(); workbook.close(); catch (IOException e) { e.printStackTrace(); catch (WriteException e) { e.printStackTrace(); jxl支持png格式图片jpg格式gif格式都支持五.插入页眉页脚般页眉页脚都三部,左,,右三部,利用代码实现插入页眉页脚java 代码public static void setHeader(WritableSheet dataSheet,String left,String center,String right){ HeaderFooter hf = new HeaderFooter(); hf.getLeft().append(left); hf.getCentre().append(center); hf.getRight().append(right); //加入页眉 dataSheet.getSettings().setHeader(hf); //加入页脚 //dataSheet.getSettings().setFooter(hf); } 我用代码测试该:java 代码try { //创建工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls")); //待插入工作表 WritableSheet dataSheet = workbook.createSheet("加入页眉",0); ExcelUtils.setHeader(dataSheet, "chb", "2007-03-06", "第1页,共3页"); workbook.write(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } 合并单元格://首先创建sheetWritableSheet sheetWrite=writeWorkbook.createSheet("sheet名称",0);// 单元格(column, row)单元格(column1, row1)进行合并sheetWrite.mergeCells(column, row, column1, row1);java 操作exceljxl加边框jxl合并单元格,单元格设置单元WritableCellFormat headerFormat = new WritableCellFormat(HEADER_FONT_STYLE);//水平居齐headerFormat.setAlignment(Alignment.CENTRE);//竖直向居齐headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//建立标签参数依:列索引、行索引、内容、格式Label seqLabel = new Label(0, 0, "序号", headerFormat);//增加单元格sheet.addCell(seqLabel);//合并单元格参数依:列索引、行索引、列索引+需要合并列数、行索引+需要合并行数sheet.mergeCells(0, 0, 0, 1);//设置单元格宽度字符单位sheet.setColumnView(0, "序号".length() + 10);Label basicInfoLabel = new Label(1, 0, "基本信息",headerFormat);sheet.addCell(basicInfoLabel);sheet.mergeCells(1, 0, 3, 0);//合并垮3行sheet.mergeCells(1, 1, 1, 3);//合并垮3列label = new Label(0, 1, "苹);sheet.mergeCells(0,1, 0, 3- 1);//合并垮3列WritableCellFormat cellFormat = new WritableCellFormat();cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直居label.setCellFormat(cellFormat);sheet.addCell(label);注:再使cellFormat需要重新NEW WritableCellFormatjava怎么操作excel合并单元格
程序中引用spire.xls.jar包
import com.spire.xls.*;
public class MergeExcels {
public static void main(String[] args){
//将待合并Excel文档的名称保存至字符串数组
String[] inputFiles = new String[]{"file1.xlsx","file2.xlsx"};
//创建一个新的Excel文档
Workbook newBook = new Workbook();
//清除默认的3张工作表
newBook.getWorksheets().clear();
//创建另一个Excel文档
Workbook tempBook = new Workbook();
//遍历数组,依次加载每个Excel文档并将文档中的所有工作表复制到新建的Excel文档中
for (String file : inputFiles)
{
tempBook.loadFromFile(file);
for (Worksheet sheet : (Iterable)tempBook.getWorksheets())
{
newBook.getWorksheets().addCopy(sheet, WorksheetCopyType.CopyAll);
}
}
//保存
newBook.saveToFile("MergeFiles.xlsx", ExcelVersion.Version2013);
}
}
/**
*
* @param context
* @param dictionary
* @param rows 数据行
* @param fileName 文件名
* @param fields 列名
* @param fieldsName 字段名
*/
private void outExcelFile(HttpContext context,
IContextDictionary dictionary, DataRowCollections rows,
String fileName, ListString fields, ListString fieldsName) {
int cellSize = fields.size();
if(cellSize == 0){
LogManager.debug("没有指定列头信息,无法导出Excel文件!");
return;
}
//============创建样式 start
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
//列头字体样式
HSSFFont headerFont = workbook.createFont();
headerFont.setFontName("宋体");
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//列头样式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headerStyle.setLocked(true);
headerStyle.setWrapText(true);
//标题样式
HSSFFont titleFont = workbook.createFont();
titleFont.setFontName("宋体");
titleFont.setFontHeightInPoints((short) 15);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setLocked(true);
titleStyle.setWrapText(true);
//普通单元格字体样式
HSSFFont cellFont = workbook.createFont();
cellFont.setFontName("宋体");
cellFont.setFontHeightInPoints((short)12);
//普通单元格样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
cellStyle.setLocked(true);
cellStyle.setWrapText(true);
//============创建样式 end
//设置序号列、列宽和标题行 start
HSSFRow titleRow = sheet.createRow(0);
titleRow.setHeightInPoints(50);
HSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellValue(new HSSFRichTextString(fileName));
titleCell.setCellStyle(titleStyle);
//sheet.addMergedRegion(new Region(0,(short)0,0,(short)cellSize));//合并单元格--方法过时
//CellRangeAddress 起始行 结束行 起始列 结束列
sheet.addMergedRegion(new CellRangeAddress(0,0,(short)0,(short)cellSize));//合并单元格
//显示总的数据个数 start
HSSFRow countRow = sheet.createRow(1);
countRow.setHeightInPoints(40);
HSSFCell countCell = countRow.createCell(0);
countCell.setCellValue(new HSSFRichTextString("共计专项检查("+rows.size()+")项"));
countCell.setCellStyle(headerStyle);
sheet.addMergedRegion(new CellRangeAddress(1,1,(short)0,(short)cellSize));//合并单元格
//显示总的数据个数 end
HSSFRow headerRow = sheet.createRow(2);
headerRow.setHeightInPoints(35);
HSSFCell headerCell = null;
//序号
int startIndex = 0 ;
headerCell = headerRow.createCell(0);
sheet.setColumnWidth(0, 2000);
headerCell.setCellValue(new HSSFRichTextString("序号"));
headerCell.setCellStyle(headerStyle);
startIndex ++ ;
//列头
for(int i = 0; i cellSize; i ++){
sheet.setColumnWidth(startIndex + i, 7000);
headerCell = headerRow.createCell(startIndex + i);
headerCell.setCellValue(new HSSFRichTextString(fields.get(i)));
headerCell.setCellStyle(headerStyle);
}
//设置序号列、列宽和标题行 end
//文件正文 start
int rowNum = 1;
int rowIndex = 0;
HSSFRow row = null;
ListInteger[] cellRangeLst = new ArrayListInteger[](0);
Integer [] arr = null;
int l = 0;
String orgName = "";
for(int j = 2; jrows.size()+2; j++){//循环行
DataRow dataRow = rows.get(rowIndex); //对应数据库字段
HSSFCell cell = null;
row = sheet.createRow(j + 1);
row.setHeightInPoints(55);
//序号
cell = row.createCell(0);
cell.setCellValue(rowNum++);
cell.setCellStyle(cellStyle);
if(StringHelper.isNullOrEmpty(orgName)){
arr = new Integer[2];
arr[0] = j + 1;
l =j + 1;
orgName = dataRow.getString("ORGNAME");
}else{
if(!orgName.equals(dataRow.getString("ORGNAME"))){
arr[1] = j;
cellRangeLst.add(arr);
sheet.addMergedRegion(new CellRangeAddress(l,j,1,1));
arr = new Integer[2];
l = j+1;
orgName = dataRow.getString("ORGNAME");
}
if(rowIndex == rows.size() - 1){
arr[1] = j+1;
cellRangeLst.add(arr);
sheet.addMergedRegion(new CellRangeAddress(l,j+1,1,1));
}
}
for(int k = 0; k cellSize; k++){
cell = row.createCell(k + startIndex);
String column = fieldsName.get(k); //对应数据库字段
String value = "";
if("APSJ".equals(column)){
value = getAPSJValue(dataRow.getString(column));
}else{
value = dataRow.getString(column);
}
cell.setCellValue(new HSSFRichTextString(value));
cell.setCellStyle(cellStyle);
}
rowIndex ++;
}
//文件正文 end
//for(Integer[] te : cellRangeLst){
// sheet.addMergedRegion(new CellRangeAddress(te[0],te[1],1,1));//合并处室单元格
//}
//下载
HttpServletResponse response = context.getResponse();
response.setContentType("application/x-download;charset=UTF-8");
String title = "export";
try {
title = java.net.URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.addHeader("Content-Disposition", "attachment;filename=" + title + ".xls");
try {
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//参考一下吧
用poi,逐个读取每个excel中的sheet,然后新建一个新的excel文件,把读出来的所有sheet加载进去就OK了,poi的具体用法参考下帖
java表格就是java swing。
//创建表头
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
//创建显示数据
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
/*
* JTable还提供了一个重载的构造方法,传入两个Vector
* JTable(Vector rowData, Vector columnNames)
*
*/
final JTable table = new JTable(data, columnNames);
table.setBackground(Color.YELLOW);
你这个是将第二excel的数据追加到第一个excel里吧。
1、你在构建第一个excel的时候,把第二excel数据读出来与源数据同时写入。
2、你可以将两个excel的数据都读出来 放到对象列表中。再将list里的对象写到excel中。