jxl导出报表时,怎样合并单元格?
我把一列数据导出,在一个循环内如何控制单元格的合并啊?
[解决办法]
问题不是很明白啊。我给你个合并单元格的
WritableFont wf_value = new jxl.write.WritableFont(WritableFont
.createFont("宋体"), 9, WritableFont.NO_BOLD);
// 表格数据样式
WritableCellFormat wcf_value = new WritableCellFormat(wf_value);
wcf_value.setAlignment(jxl.format.Alignment.CENTRE);
wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_value.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcf_value.setWrap(true);
int startRowNum=0;//起始行
int startColNum=0;//起始列
ws.addCell(new Label(startColNum,startRowNum,"项目汇总",wcf_value));
ws.mergeCells(startColNum,startRowNum, startColNum+14,startRowNum);
startRowNum++;
看下这段代码。是我项目里面拷贝出来的
自己调节下下面方法的参数值就知道了
ws.mergeCells(startColNum,startRowNum, startColNum+14,startRowNum);
[解决办法]
package Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import jxl.Workbook;
import jxl.format.Alignment;
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 Test {
public static void main(String[] args) throws WriteException, IOException {
WritableWorkbook wb = null;
try {
WritableFont wf_key = new jxl.write.WritableFont(WritableFont
.createFont("宋体"), 12, WritableFont.BOLD);
WritableFont wf_value = new jxl.write.WritableFont(WritableFont
.createFont("宋体"), 9, WritableFont.NO_BOLD);
WritableFont wvf_value = new jxl.write.WritableFont(WritableFont
.createFont("宋体"), 9, WritableFont.BOLD);
// 表格数据样式
WritableCellFormat wcf_value = new WritableCellFormat(wf_value);
wcf_value.setAlignment(jxl.format.Alignment.CENTRE);
wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_value.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcf_value.setWrap(true);
// 表格数据样式
WritableCellFormat wcfNum_value = new WritableCellFormat(wf_value);
wcfNum_value.setAlignment(jxl.format.Alignment.RIGHT);
wcfNum_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcfNum_value.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcfNum_value.setWrap(true);
WritableCellFormat wc_value = new WritableCellFormat(wvf_value);
wc_value.setAlignment(jxl.format.Alignment.CENTRE);
wc_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wc_value.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wc_value.setWrap(true);
WritableCellFormat wvc_value = new WritableCellFormat(wvf_value);
wvc_value.setAlignment(jxl.format.Alignment.LEFT);
wvc_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wvc_value.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wvc_value.setWrap(true);
// 表头样式
WritableCellFormat wcf_key = new WritableCellFormat(wf_key);
wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
wcf_key.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
wcf_key.setWrap(true);
// 表名样式
WritableCellFormat wcf_name_right = new WritableCellFormat(wf_key);
wcf_name_right.setAlignment(Alignment.RIGHT);
WritableCellFormat wcf_name_center = new WritableCellFormat(wf_key);
wcf_name_center.setAlignment(Alignment.CENTRE);
WritableCellFormat wcf_name_left = new WritableCellFormat(wf_key);
wcf_name_left.setAlignment(Alignment.LEFT);
// 页名称样式
WritableFont wf_title = new jxl.write.WritableFont(WritableFont
.createFont("微软雅黑"), 14, WritableFont.BOLD);
WritableCellFormat wcf_title = new WritableCellFormat(wf_title);
wcf_title.setAlignment(Alignment.CENTRE);
OutputStream out =new FileOutputStream("C:\\a.xls");
wb = Workbook.createWorkbook(out);//这里的out是传过来的,是response.getOutputStream()得到的。这里我构造一个
WritableSheet ws = wb.createSheet("经销表", 0);
ws.setColumnView(0,10);
ws.setColumnView(1,25);
ws.setColumnView(2,20);
ws.setColumnView(3,8);
ws.setColumnView(4,8);
ws.setColumnView(5,25);
ws.setColumnView(6,11);
ws.setColumnView(7,11);
ws.setColumnView(8,11);
ws.setColumnView(9,11);
ws.setColumnView(10,11);
ws.setColumnView(11,11);
ws.setColumnView(12,30);
int startRowNum=0;//起始行
int startColNum=0;//起始列
ws.addCell(new Label(startColNum,startRowNum,"项目名称:",wvc_value));
ws.mergeCells(startColNum,startRowNum, startColNum+5,startRowNum+2);
wb.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (wb != null) {
wb.close();
wb = null;
}
}
}
}
拿这个试试你就明白了