「java合并excel」java合并excel单元格计数问题

博主:adminadmin 2022-12-06 06:09:09 106

今天给各位分享java合并excel的知识,其中也会对java合并excel单元格计数问题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java excel 怎么合并大的单元格

POIexcel表格何合并单元格

Java代码

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;

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 org.apache.poi.hssf.util.Region;

public class ExcelTest {

/**

* @param args

*/

public static void main(String[] args) throws IOException {

try {

HSSFWorkbook wb = new HSSFWorkbook();

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

HSSFCellStyle style = wb.createCellStyle(); // 式象

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平

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

HSSFRow row2 = sheet.createRow((short) 1);

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

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

ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 文处理

ce.setCellValue("项目\\期"); // 表格第行第列显示数据

ce.setCellStyle(style); // 式居

int num = 0;

for (int i = 0; i 9; i++) { // 循环9每都要跨单元格显示

// 计算单元格跨格

int celln = 0;

int celle = 0;

if (i == 0) {

celln = 0;

celle = 1;

} else {

celln = (i * 2);

celle = (i * 2 + 1);

}

// 单元格合并

// 四参数别:起始行起始列结束行结束列

sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,

(short) (celle + 1)));

HSSFCell cell = row.createCell((short) (celln + 1));

cell.setCellValue("merging" + i); // 跨单元格显示数据

cell.setCellStyle(style); // 式

// 跨单元格显示数据:两行行别两格格行两格数量金额

HSSFCell cell1 = row2.createCell((short) celle);

HSSFCell cell2 = row2.createCell((short) (celle + 1));

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

cell1.setCellValue("数量");

cell1.setCellStyle(style);

cell2.setEncoding(HSSFCell.ENCODING_UTF_16);

cell2.setCellValue("金额");

cell2.setCellStyle(style);

num++;

}

// 面加合计百比

// 合计 加要跨单元格

sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,

(short) (2 * num + 2)));

HSSFCell cell = row.createCell((short) (2 * num + 1));

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue("合计");

cell.setCellStyle(style);

HSSFCell cell1 = row2.createCell((short) (2 * num + 1));

HSSFCell cell2 = row2.createCell((short) (2 * num + 2));

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

cell1.setCellValue("数量");

cell1.setCellStyle(style);

cell2.setEncoding(HSSFCell.ENCODING_UTF_16);

cell2.setCellValue("金额");

cell2.setCellStyle(style);

// 百比 同

sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,

(short) (2 * num + 4)));

HSSFCell cellb = row.createCell((short) (2 * num + 3));

cellb.setEncoding(HSSFCell.ENCODING_UTF_16);

cellb.setCellValue("百比");

cellb.setCellStyle(style);

HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));

HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));

cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);

cellb1.setCellValue("数量");

cellb1.setCellStyle(style);

cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);

cellb2.setCellValue("金额");

cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

System.out.print("OK");

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

java如何如何实现两张excel表的合并?

你这个是将第二excel的数据追加到第一个excel里吧。

1、你在构建第一个excel的时候,把第二excel数据读出来与源数据同时写入。

2、你可以将两个excel的数据都读出来 放到对象列表中。再将list里的对象写到excel中。

java怎么操作excel合并单元格

利用java操作Excel源东东-jxl.jar读取前单元格值 String cellValue = cells[k].getContents(); sb.append(cellValue+" "); } } sb.append("

"); } 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合并单元格

poi Java生成excel合并单元格后字体居中

excel表格中合并的单元格内的字居中到两行之间的位置:

1、选中合并后的单元格

2、在工具菜单栏选择

格式

在下拉菜单选择单元格

3、在单元格格式选项卡下面选择对齐

4、在文本对齐方式中水平对齐

选择居中,在垂直对齐下面选择居中。确定。后单元格内的字就会居中到两行之间的位置了。

java 怎么样把多个Excel 合并为一个

如果要把多个合在一起,直接进行文件合并是不现实的。提供一种思路,把所有的excel内容读取出来,然后在写入同一个excel文件中。

aspose.cells java合并excel

using System; using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.IO;

using System.Data;

using Aspose.Cells;

/// summary

///OutFileDao 的摘要说明

/// /summary

public class OutFileDao

{

public OutFileDao()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

/// summary

/// 测试程序

/// /summary

public static void testOut()

{

DataTable dt = new DataTable();

dt.Columns.Add("name");

dt.Columns.Add("sex");

DataRow dr = dt.NewRow();

dr["name"] = "名称1";

dr["sex"] = "性别1";

dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();

dr1["name"] = "名称2";

dr1["sex"] = "性别2";

dt.Rows.Add(dr1);

OutFileToDisk(dt, "测试标题", @"d:\测试.xls");

}

/// summary

/// 导出数据到本地

/// /summary

/// param name="dt"要导出的数据/param

/// param name="tableName"表格标题/param

/// param name="path"保存路径/param

public static void OutFileToDisk(DataTable dt,string tableName,string path)

{

Workbook workbook = new Workbook(); //工作簿

Worksheet sheet = workbook.Worksheets[0]; //工作表

Cells cells = sheet.Cells;//单元格

//为标题设置样式

Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式

styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中

styleTitle.Font.Name = "宋体";//文字字体

styleTitle.Font.Size = 18;//文字大小

styleTitle.Font.IsBold = true;//粗体

//样式2

Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式

style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中

style2.Font.Name = "宋体";//文字字体

style2.Font.Size = 14;//文字大小

style2.Font.IsBold = true;//粗体

style2.IsTextWrapped = true;//单元格内容自动换行

style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

//样式3

Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式

style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中

style3.Font.Name = "宋体";//文字字体

style3.Font.Size = 12;//文字大小

style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

int Colnum = dt.Columns.Count;//表格列数

int Rownum=dt.Rows.Count;//表格行数

//生成行1 标题行

cells.Merge(0, 0, 1, Colnum);//合并单元格

cells[0, 0].PutValue(tableName);//填写内容

cells[0, 0].SetStyle(styleTitle);

cells.SetRowHeight(0, 38);

//生成行2 列名行

for (int i = 0; i Colnum; i++)

{

cells[1, i].PutValue(dt.Columns[i].ColumnName);

cells[1, i].SetStyle(style2);

cells.SetRowHeight(1, 25);

}

//生成数据行

for (int i = 0; i Rownum; i++)

{

for (int k = 0; k Colnum; k++)

{

cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());

cells[2 + i, k].SetStyle(style3);

}

cells.SetRowHeight(2+i, 24);

}

workbook.Save(path);

}

public MemoryStream OutFileToStream(DataTable dt, string tableName)

{

Workbook workbook = new Workbook(); //工作簿

Worksheet sheet = workbook.Worksheets[0]; //工作表

Cells cells = sheet.Cells;//单元格

//为标题设置样式

Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式

styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中

styleTitle.Font.Name = "宋体";//文字字体

styleTitle.Font.Size = 18;//文字大小

styleTitle.Font.IsBold = true;//粗体

//样式2

Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式

style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中

style2.Font.Name = "宋体";//文字字体

style2.Font.Size = 14;//文字大小

style2.Font.IsBold = true;//粗体

style2.IsTextWrapped = true;//单元格内容自动换行

style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

//样式3

Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式

style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中

style3.Font.Name = "宋体";//文字字体

style3.Font.Size = 12;//文字大小

style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

int Colnum = dt.Columns.Count;//表格列数

int Rownum = dt.Rows.Count;//表格行数

//生成行1 标题行

cells.Merge(0, 0, 1, Colnum);//合并单元格

cells[0, 0].PutValue(tableName);//填写内容

cells[0, 0].SetStyle(styleTitle);

cells.SetRowHeight(0, 38);

//生成行2 列名行

for (int i = 0; i Colnum; i++)

{

cells[1, i].PutValue(dt.Columns[i].ColumnName);

cells[1, i].SetStyle(style2);

cells.SetRowHeight(1, 25);

}

//生成数据行

for (int i = 0; i Rownum; i++)

{

for (int k = 0; k Colnum; k++)

{

cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());

cells[2 + i, k].SetStyle(style3);

}

cells.SetRowHeight(2 + i, 24);

}

MemoryStream ms = workbook.SaveToStream();

return ms;

}

}

关于java合并excel和java合并excel单元格计数问题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

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