excel 表头合并 和拆分的例子
2017-08-02 10:19
239 查看
自己整理了一个excel 表头合并 和拆分的例子
import java.io.File; import java.io.IOException; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class Test { public static void main(String[] args) throws Exception { String path = "e:/test.xlsx"; writeExcel(path); } public static void writeExcel(String fileName) throws IOException, RowsExceededException, WriteException { // 构建一个工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName)); if (workbook == null) { return; } // 获取第一个sheet WritableSheet sheet = workbook.createSheet("sheet1", 0); if (sheet == null) { return; } WritableFont bold = new WritableFont(WritableFont.createFont("微软雅黑"), 12, WritableFont.NO_BOLD); WritableCellFormat titleFormate = new WritableCellFormat(bold);// 生成一个单元格样式控制对象 titleFormate.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);// 设置边框 titleFormate.setAlignment(jxl.format.Alignment.CENTRE);// 单元格中的内容水平方向居中 titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中 // 创建表头 sheet.mergeCells(0, 0, 0, 1);// 单元格合并 sheet.addCell(new Label(0, 0, "时间", titleFormate)); sheet.mergeCells(1, 0, 4, 0); sheet.addCell(new Label(1, 0, "新增积分总数", titleFormate)); sheet.addCell(new Label(1, 1, "交易", titleFormate)); sheet.addCell(new Label(2, 1, "扫码", titleFormate)); sheet.addCell(new Label(3, 1, "爱互动", titleFormate)); sheet.addCell(new Label(4, 1, "合计", titleFormate)); sheet.mergeCells(5, 0, 5, 1); sheet.addCell(new Label(5, 0, "消费积分总数", titleFormate)); sheet.mergeCells(6, 0, 11, 0); sheet.addCell(new Label(6, 0, "剩余有效积分总数", titleFormate)); sheet.addCell(new Label(6, 1, "店铺会员", titleFormate)); sheet.addCell(new Label(7, 1, "中级会员", titleFormate)); sheet.addCell(new Label(8, 1, "高级会员", titleFormate)); sheet.addCell(new Label(9, 1, "VIP会员", titleFormate)); sheet.addCell(new Label(10, 1, "SVIP会员", titleFormate)); sheet.addCell(new Label(11, 1, "合计", titleFormate)); sheet.mergeCells(12, 0, 12, 1); sheet.addCell(new Label(12, 0, "过期积分数", titleFormate)); WritableCellFormat bodyFormate = new WritableCellFormat();// 生成一个单元格样式控制对象 bodyFormate.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);// 设置边框 for(int i = 2; i <= 12; i++){ sheet.addCell(new Label(0, i, "2017-04-08", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); sheet.addCell(new Label(0, i, "0", bodyFormate)); } // 设置单元格的宽度 sheet.setColumnView(5, 20); sheet.setColumnView(6, 11); sheet.setColumnView(7, 11); sheet.setColumnView(8, 11); sheet.setColumnView(9, 11); sheet.setColumnView(10, 11); sheet.setColumnView(11, 11); sheet.setColumnView(12, 20); // 从内存中写入文件中 workbook.write(); // 关闭资源,释放内存 workbook.close(); } }
相关文章推荐
- Excel 2007单元格及内容的合并、拆分_C#教程
- 写了个datagridview合并表头的例子,请多指教!
- Excel 2007单元格及内容的合并、拆分_C#教程
- Excel应用技巧:单元格文字的拆分与合并
- Excel应用技巧:单元格文字的拆分与合并
- excel技巧--文本拆分合并
- Easyui+Spring Mvc导出Excel:支持按模板样式导出、合并表头、合并单元格、多sheet
- 如何合并多个excel报表到同一表中,只保留一个表头
- C#导出Excel——多表头、表头跨行跨列合并
- 合并Excel报表复杂表头为一行显示
- html中表头的合并,类Excel
- Java Web利用POI导出Excel简单例子
- excel关于邮件合并的打印
- VB6.0 用Excel生成數據分析餅圖例子
- 动态合并DataGrid表头栏位
- ASP.NET 2.0中合并 GridView 的表头单元格
- 如何合并和拆分文本
- ASP.NET 2.0中合并 GridView 的表头单元格
- 通道的拆分与合并
- C#合并多个结构一样的Excel