我正在使用Poi.jar从excel表中获取输入,并想知道如何检查单元格是否为空.
现在我使用下面的代码.
cell = myRow.getCell(3);
if (cell != null) {
    cell.setCellType(Cell.CELL_TYPE_STRING);
    //System.out.print(cell.getStringCellValue() + "\t\t");
    if (cell.getStringCellValue() != "")
        depend[p] = Integer.parseInt(cell.getStringCellValue());
    }
}
我在java中使用Apache POI来创建excel文件.我填写数据然后尝试自动调整每列,但是大小总是错误的(我认为一致).前两行总是(?)完全折叠.当我在excel中自动调整列时,它可以完美地工作.
没有写入空白单元格(我相信),调整大小是我做的最后一件事.
这是相关的代码:这是一个没有错误处理等的简化版本.
public static synchronized String storeResults(ArrayList<String> resultList, String file) {
    if (resultList == null || resultList.size() == 0) {
        return file;
    }
    FileOutputStream stream = new FileOutputStream(file);
    //Create workbook and result sheet
    XSSFWorkbook book = new XSSFWorkbook();
    Sheet results = book.createSheet("Results");
    //Write results to workbook
    for (int x = 0; x < resultList.size(); x++) {
        String[] items = resultList.get(x).split(PRIM_DELIM);
        Row row = results.createRow(x);
        for (int i = 0; i < items.length; i++) …我有一个文件的InputStream,我使用apache poi组件来读取它像这样:
POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
问题是我需要多次使用相同的流,POIFSFileSystem在使用后关闭流.
从输入流缓存数据然后将更多输入流提供给不同的POIFSFileSystem的最佳方法是什么?
编辑1:
通过缓存我的意思是存储供以后使用,而不是作为加速应用程序的方法.最好是将输入流读入数组或字符串,然后为每次使用创建输入流?
编辑2:
很抱歉重新打开这个问题,但在桌面和Web应用程序中工作时条件有所不同.首先,我从tomcat web app中的org.apache.commons.fileupload.FileItem获取的InputStream不支持标记,因此无法重置.
其次,我希望能够将文件保存在内存中,以便在处理文件时更快地访问和减少io问题.
我想使用Apache POI库来解析excel文件(旧版本和更新版本的excel).所以我想知道我需要从Apache POI中包含哪些罐子,因为在以下链接中:
http://mvnrepository.com/artifact/org.apache.poi
我发现要包含很多罐子,我需要将它们全部包括在内吗?
如果是这样,最新的稳定版本是什么,它是否与微软的Office 2010一起使用?
我是Apache POI api的初学者.我正在尝试使用arraylist创建excel表.
我的java代码如下.
HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
     HSSFCellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIME.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFRow row4 = sheet.createRow(4);
    row4.createCell(4).setCellValue("name");
    row4.createCell(5).setCellValue("emailId");
    sheet.autoSizeColumn(5);
    List<Bean> nameList = this.getArrayList();
    Iterator<Bean> nameListIterator = nameList.iterator();
    sheet.autoSizeColumn(5);
    int i=5;
    HSSFRow row = null;
    while(nameListIterator.hasNext())
    {
        Bean bean = nameListIterator.next();
        row = sheet.createRow(i);
        row.createCell(4).setCellValue(bean.getName());
        row.createCell(5).setCellValue(bean.getMailId());
        i++;
    }
arraylist如下:
List<Bean> beanList = new ArrayList<Bean>();
    beanList.add(new Bean("Amy","g@y.comrtyrtyrtyrtyrtyr"));
    beanList.add(new Bean("Joan","p@y.comrtyrtyrtyrtyrtyr"));
    beanList.add(new Bean("Megan","r@y.comrtyrtyrtyrtyrtyr"));
    beanList.add(new Bean("Joe","m@y.comrtyrtyrtyrtyrtyr"));
    beanList.add(new Bean("Febi","j@y.comrtyrtyrtyrtyrtyr"));
生成Excel工作表时,该列不能正确匹配内容的大小.我搜索了谷歌相关的这个问题并找到了
sheet.autoSizeColumn(5);
是我的问题的解决方案.我在上面的代码中添加了,但问题仍然存在.我正确使用它吗?
还有其他解决方案吗?
请帮忙
提前致谢
Ps:我正在使用Apache Poi 3.6
我想使用Apache poi将数据导出到excel .
现在我遇到的问题是我无法合并行并将它们对齐在中心.
出口数据代码是:
List<LinkedHashMap<String,Object>> lstReportHeader = null;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
//Set Header Font
HSSFFont headerFont = wb.createFont();
headerFont.setBoldweight(headerFont.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short) 12);
//Set Header Style
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
headerStyle.setAlignment(headerStyle.ALIGN_CENTER);
headerStyle.setFont(headerFont);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
int rowCount= 0;
Row header;
header = sheet.createRow(0);//its for header 
Cell cell ;//= header.createCell(0);
for(int j = 0;j < 4; j++) {
    cell = header.createCell(j);
    if(j == 0) {
        cell.setCellValue("ItemWise List");
    }
    cell.setCellStyle(headerStyle);
}
sheet.addMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, lstReportFormHeader.size()-1)); …如何在poi的帮助下在单元格中写出我的全部价值?
即如果值是1000.000,那么如何在不"#"之后截断000的情况下写出这个完整的值.与POI?意味着我想要全部价值.
在我的情况下,它只需要1000,但这对我来说不是正确的格式.
我正在使用Apache POI(XSSF API)来读取xlsx文件.当我试图读取file.i时出现以下错误:
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
码:
public class ReadXLSX
{
private String filepath;
private XSSFWorkbook workbook;
private static Logger logger=null;
private  InputStream resourceAsStream;
public ReadXLSX(String FilePath)
{
    logger=LoggerFactory.getLogger("ReadXLSX");
    this.filepath=FilePath;
    resourceAsStream = ClassLoader.getSystemResourceAsStream(filepath);
}
public ReadXLSX(InputStream fileStream)
{ 
    logger=LoggerFactory.getLogger("ReadXLSX");
    this.resourceAsStream=fileStream;
}
private void loadFile() throws FileNotFoundException, NullObjectFoundException
{
    if(resourceAsStream==null)
        throw new FileNotFoundException("Unable to locate give file..");
    else
    {
        try
        {
           workbook = new XSSFWorkbook(resourceAsStream);
        }
        catch(IOException ex)
        {
        }
    }
}// end loadxlsFile …我正在使用Apache POI读取部件号电子表格中的数据.我在我们的数据库中查找零件编号,如果我们有零件的CAD图纸我将零件编号单元格颜色为绿色,如果我们不对其进行颜色红色.处理完成后,将保存电子表格.我遇到的问题是该列中的每个单元格都是绿色的.我已经逐步完成了代码,查找部件号的逻辑工作正常,确定单元应该是什么颜色以及设置颜色和填充的逻辑似乎也可以工作.我在这里做错了什么想法?
谢谢.
//Check the parts
for(int r=1;r<sheet.getPhysicalNumberOfRows();r++) {
    String partNumber = null;
    switch(cell.getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:
            long pNum = (long) cell.getNumericCellValue();
            partNumber = String.valueOf(pNum);
            break;
        case HSSFCell.CELL_TYPE_STRING:
            partNumber = cell.getStringCellValue();
            break;
        default:
            logger.info("Part Number at row " + r + " on sheet " + partList.getSheetName(s) + "is of an unsupported type");
    }
    try {
        List<String> oldMaterialNumbers = getOldMaterialNumbers(partNumber);
        boolean gotDrawing = checkPartNumber(oldMaterialNumbers, partNumber);
        //If there's a drawing then color the row green, if not red.
        short bgColorIndex = …