Tag: apache poi

将样式从一个Excel工作簿复制到另一个

我想将一张工作簿(包括样式)复制到一个新的工作簿中。 我试着遍历所有单元格和 CellStyle newCellStyle = workbook.createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); newCell.setCellStyle(newCellStyle); 抛出java.lang.IllegalStateException:超过了单元格样式的最大数量。 您可以在.xls工作簿中定义最多4000个样式 CellStyle newCellStyle = oldCell.getCellStyle(); newCell.setCellStyle(newCellStyle); 抛出java.lang.IllegalArgumentException:该样式不属于提供的工作簿。 你是否试图从一个工作簿中分配一个样式到一个不同工作簿的单元格? 什么是复制样式的正确方法?

删除最后一行Excel中的错误 – Apache POI

我正在尝试使用Apache-POI删除最后10行。 我试图find解决scheme在线,没有运气。 我的代码: import java.io.*; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.sl.usermodel.Sheet; import org.apache.poi.xssf.usermodel.*; public class Sample1 { public static void main(String[] args)throws Exception { File file = new File("File.xlsx"); FileInputStream fIP = new FileInputStream(file); XSSFWorkbook wb = new XSSFWorkbook(fIP); XSSFSheet sheet = wb.getSheetAt(1); int lastRowNum = sheet.getLastRowNum()+1; //XSSFRow removingRow = sheet.getRow(lastRowNum); if(file.isFile() && file.exists()) { for (int […]

在Java中使用poi与中文字符写.xlsx文件

我正在尝试使用apache poi创build.xlsx文件。 这是工作正常与阿尔巴尼亚语和印地文等字符。 但我在我的应用程序,我们也使用汉字。 当我使用我的代码创build.xlsx文件时,只显示中文字符。 我正在使用此代码来创build.xlsx文件。 Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow(1); // Create a cell and put a value in it. Cell cell = row.createCell(1); //arbaric char set in cell and show […]

使用XLSX Apache poi的Java临时文件

.hi,我正在尝试创build一个临时的.xlsx文件,并使用apache poi写入。 我得到EmptyFileException创build工作簿。 这是代码: public class Writer{ File file; public File Write(List<FormData> l, Class elementClass) { try {//create temp fiele here file = File.createTempFile(elementClass.getSimpleName(),".xlsx"); } catch (IOException ex) { Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex); } XSSFWorkbook workbook; XSSFSheet sheet; if (file.exists()) { FileInputStream inputStream; try { inputStream = new FileInputStream(file); } catch (FileNotFoundException ex) { throw new […]

POI Java将xlsx转换为CSV逗号和双引号

尝试将xlsx转换为CSV,其中一列有多个引号和逗号,当转换为CSV时,它被分隔到不同的单元格而不是一个单元格。 以下是场景, 标题正在移动到多个,附上代码 if(cell.getStringCellValue().contains(",")){ if (cell.getStringCellValue().contains("\"")){ String t= "\""+cell.getStringCellValue()+"\""; data.append(t+","); } else{ data.append("\""+cell.getStringCellValue()+"\""+","); } }

Selenium中的数据提供者与TestNG不匹配

我正在下面的代码写在selenium和下面的错误显示,请让我知道问题在哪里。 import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class testngexcel { public static ExcelReader excel = null; @Test(dataProvider = "newdata") public void testData(String username, String password, Integer age) { System.out.println(username + " – " + password + " – " + age); } @DataProvider(name = "newdata") public static Object[][] getData() { if (excel == null) { excel […]

如何将单元格的背景颜色设置为任何RGB值?

我试图设置一个单元格背景颜色像这样的RGB值 XSSFCellStyle instructionStyle = wb.createCellStyle(); instructionStyle.setWrapText(true); XSSFColor myColor = new XSSFColor(new java.awt.Color(0, 73,144)); instructionStyle.setFillForegroundColor(myColor); instructionStyle.setAlignment(HorizontalAlignment.CENTER); 甚至它编译的水平alignment中心没有其他的效果,单元格的look.can有人告诉我什么是错误的代码?谢谢

如何使用Apache POI(SXSSF)为特定单元格设置数据(数字)格式区域设置?

问题是非常具体的:使用Apache POI,我想创build单元格(完成),为其分配数字格式(完成),并设置格式的语言环境( 卡在这里 )。 代码如下所示: SXSSFWorkbook workbook = new SXSSFWorkbook(100); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(1); Cell cell = row.createCell(0); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(8); //currency with thousands separator and two decimal places after period cell.setCellValue(123456.78); //??? how to set custom locale for the cell's number format? 我试图解决与自定义区域设置的问题是自定义千位分隔符字符(法国的非rest空间对我来说是好的)。 XLSX工作簿允许这样的自定义(更新:我的意思是设置格式区域设置每个单元格),这是可以实现与MS Office和OpenOffice。 我想在代码中做同样的事情。 (Apache POI […]

为什么不能使用Apache POI“正确”隐藏Excel行?

为什么不可能使用Apache POI(3.16)“正确”隐藏Excel行? 可以调用(XSSFRow)row.setZeroHeight(),这也是繁忙的开发人员指南build议。 但是,这不像Excel那样隐藏行。 您可以使用相应的上下文菜单选项“隐藏”和“取消隐藏”行。 我认为设置行风格应该工作,但它不。 在生成的Excel文件中,行仍然可以看到。 package de.mwe; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.testng.Assert; import org.testng.annotations.Test; public class MWE { @Test public void testHidingRows() { final XSSFWorkbook wb = new XSSFWorkbook(); String sname = "HideRowsTestSheet", cname = "TestName", cvalue = "TestVal"; XSSFSheet […]

Apache POI复制具有n次公式的行

我有一个6列和50k行的表。 我想复制一行n次。 现在我正在这样做: for (int i=0; i<=excel_max_row; i++) { row = sheet3.createRow((short) i); cell = row.createCell(0); cell.setCellFormula("IFERROR(LOOKUP(Template!A"+(i+2)+",'Dropdown-Values'!A:A,'Dropdown-Values'!B:B),\"\")"); cell = row.createCell(1); cell.setCellFormula("IF(Template!B"+(i+2)+"=0,\"\",Template!B"+(i+2)+")"); cell = row.createCell(2); cell.setCellFormula("IFERROR(LOOKUP(Template!C"+(i+2)+",'Dropdown-Values'!C:C,'Dropdown-Values'!D:D),\"\")"); cell = row.createCell(3); cell.setCellFormula("IFERROR(LOOKUP(Template!D"+(i+2)+",'Dropdown-Values'!E:E,'Dropdown-Values'!F:F),\"\")"); cell = row.createCell(4); cell.setCellFormula("IFERROR(LOOKUP(Template!E"+(i+2)+",'Dropdown-Values'!E:E,'Dropdown-Values'!F:F),\"\")"); cell = row.createCell(5); cell.setCellFormula("IF(Template!F"+(i+2)+"=0,\"\",Template!F"+(i+2)+")"); } XSSFFormulaEvaluator.evaluateAllFormulaCells(wb); 它的工作原理很慢,因为excel_max_row可以达到50k。 有没有办法快速复制连续n次所有的Forumals? 非常感谢