「查询附件列表java」附件列表内容

博主:adminadmin 2022-11-28 04:08:07 47

今天给各位分享查询附件列表java的知识,其中也会对附件列表内容进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用Java编写程序读入附件Excel文件中的表格数据,然后用命令行方式输出所有数据

/**

*

*只对你的Excel 有效,其他不能保证

* 示例代码,不能直接用到生产环境

/

package readExcel;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

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

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

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.DateUtil;

import org.apache.poi.ss.usermodel.Row;

public class ReadXLSTools {

    ListDataModel result = new ArrayListDataModel();

    Row firstRow;

    Row secondRow;

    /**

     * 

     * @param path

     *            --tell us where is the xls file

     * 

     *            please don't pass a folder path.

     * @throws FileNotFoundException

     */

    public void read(String path) throws FileNotFoundException {

        File xlsFile = new File(path);

        if (!xlsFile.exists()) {

            throw new IllegalArgumentException(

                    "The source file cannot be found.");

        }

        if (xlsFile.isDirectory()) {

            throw new IllegalArgumentException("Source file can't be a folder!");

        }

        openXLS(xlsFile);

    }

    private void openXLS(File xlsFile) throws FileNotFoundException {

        InputStream is = new FileInputStream(xlsFile);

        try {

            HSSFWorkbook xlsWorkBooks = new HSSFWorkbook(is);

            HSSFSheet firstSheet = xlsWorkBooks.getSheetAt(0);

            for (Row row : firstSheet) {

                if (row.getRowNum() == 0) {

                    firstRow = row;

                    continue;

                }

                if (row.getRowNum() == 1) {

                    secondRow = row;

                    continue;

                }

                for (Cell cell : row) {

                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

                        Double value = (Double) getCellValue(cell);

                        if (value == 0.0) {

                            continue;

                        }

                        DataModel data = new DataModel();

                        data.project = String.valueOf(getCellValue(row

                                .getCell(0)));

                        int organIndex = cell.getColumnIndex();

                        Cell organCell = firstRow.getCell(organIndex);

                        String organ = String.valueOf(getCellValue(organCell));

                        if ("".equals(organ)) {

                            organ = String.valueOf(getCellValue(firstRow

                                    .getCell(organIndex - 1)));

                        }

                        data.organ = organ;

                        String item = String.valueOf(getCellValue(secondRow

                                .getCell(organIndex)));

                        data.item = item;

                        data.value = value;

                        result.add(data);

                    }

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    private Object getCellValue(Cell cell) {

        switch (cell.getCellType()) {

            case Cell.CELL_TYPE_STRING:

                return cell.getRichStringCellValue().getString();

            case Cell.CELL_TYPE_NUMERIC:

                if (DateUtil.isCellDateFormatted(cell)) {

                    return cell.getDateCellValue();

                } else {

                    return cell.getNumericCellValue();

                }

            case Cell.CELL_TYPE_BOOLEAN:

                return cell.getBooleanCellValue();

            case Cell.CELL_TYPE_FORMULA:

                return cell.getCellFormula();

            default:

                return "";

        }

    }

    public static void main(String[] args) {

        try {

            ReadXLSTools tool = new ReadXLSTools();

            tool.read("d:\\test\\test.xls");

            for (int i = 0; i  tool.result.size(); i++) {

                System.out.println(tool.result.get(i));

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

    }

}

package readExcel;

public class DataModel {

    String organ;

    String project;

    String item;

    double value;

 

    @Override

    public String toString(){

        return "["+project+","+organ+"/"+item+"]"+value;

    }

    

    

}

怎样在Java里取得满足条件的文件列表

Java通过File.listFiles/list方法来列出目录下的文件列表。下面介绍File.listFiles方法的用法。File.list的用法也基本一样。

File.listFiles方法有三种形式,

public File [] listFiles()

无参数的listFiles将返回所有文件,包括子文件与子目录。

public File [] listFiles(java.io.FilenameFilter)

java.io.FilenameFilter:文件名过滤器接口。过滤器必须实现此接口。该接口定义了一个

public boolean accept(File file, String filename)方法,第一个参数File

file为正在被过滤的文件,第二个参数为正在被过滤的文件名。FilenameFilter.accept返回false的文件会被过滤掉。

该方法返回匹配FilenameFilter所指定条件的文件

public File [] listFiles(java.io.FileFilter)

public boolean accept(File file)方法,第一个参数File file为正在被过滤的文件。FileFilter.accept返回false的文件会被过滤掉。

该方法返回匹配FileFilter所指定条件的文件。

下面我们通过举例说明后2个方法的用法。

取得指定扩展名的文件列表:

public static FilenameFilter getFileExtensionFilter(String extension) {

final String _extension = extension;

return new FilenameFilter() {

public boolean accept(File file, String name) {

boolean ret = name.endsWith(_extension);

return ret;

}

};

}

File file = new File("c:\\");

File[] zipFiles = file.listFiles(getFileExtensionFilter(".zip"));

public static FilenameFilter getFileExtensionFilter(String extension) { final String _extension = extension; return new FilenameFilter() { public boolean accept(File file, String name) { boolean ret = name.endsWith(_extension); return ret; } }; } File file = new File("c:\\"); File[] zipFiles = file.listFiles(getFileExtensionFilter(".zip"));

取得文件名满足所指定的规则表达式的文件列表

public static FilenameFilter getFileRegexFilter(String regex) {

final String regex_ = regex;

return new FilenameFilter() {

public boolean accept(File file, String name) {

boolean ret = name.matches(regex_);

return ret;

}

};

}

File file = new File("c:\\");

//取得文件名为8个数字,扩展名为.html的文件

File[] numberHtmlFiles = file.listFiles(getFileRegexFilter("[0-9]{8}\\.html"));

public static FilenameFilter getFileRegexFilter(String regex) { final String regex_ = regex; return new FilenameFilter() { public boolean accept(File file, String name) { boolean ret = name.matches(regex_); return ret; } }; } File file = new File("c:\\"); //取得文件名为8个数字,扩展名为.html的文件 File[] numberHtmlFiles = file.listFiles(getFileRegexFilter("[0-9]{8}\\.html"));

取得非目录的文件列表:

public static FileFilter getNotDirFileFilter() {

return new FileFilter() {

public boolean accept(File file) {

return file.isFile();

}

};

}

File file = new File("c:\\");

File[] notDirFiles = file.listFiles(getNotDirFileFilter());

如何用Java实现文件列表?

用递归实现。

1. import java.io.File;

2. import java.io.FileFilter;

3. import java.io.FilenameFilter;

4.

5. /**

6. * 文件列表。br

7. * 可以指定过滤条件。

8. *

9. * @author leo

10. *

11. */

12. public class FileList {

13.

14. /**

15. * @param args

16. */

17. public static void main(String[] args) {

18. File dir = new File(".");

19.

20. // 所有的文件和目录名

21. String[] children = dir.list();

22. if (children == null) {

23. // 不存在或者不是目录

24. } else {

25. System.out.println("#### 1 ####");

26. for (int i = 0; i children.length; i++) {

27. // 文件名

28. System.out.println(children[i]);

29. }

30. }

31.

32. // 可以指定返回文件列表的过滤条件

33. // 这个例子不返回那些以.开头的文件名

34. FilenameFilter filter = new FilenameFilter() {

35. public boolean accept(File dir, String name) {

36. return !name.startsWith(".");

37. }

38. };

39. children = dir.list(filter);

40. System.out.println("#### 2 ####");

41. for (int i = 0; i children.length; i++) {

42. // 文件名

43. System.out.println(children[i]);

44. }

45.

46. // 也可以拿到文件对象的列表

47. File[] files = dir.listFiles();

48. System.out.println("#### 3 ####");

49. for (int i = 0; i files.length; i++) {

50. // 文件名

51. System.out.println(files[i].getName());

52. }

53.

54. // 下面这个过滤条件只返回目录

55. FileFilter fileFilter = new FileFilter() {

56. public boolean accept(File file) {

57. return file.isDirectory();

58. }

59. };

60. files = dir.listFiles(fileFilter);

61. System.out.println("#### 4 ####");

62. for (int i = 0; i files.length; i++) {

63. // 文件名

64. System.out.println(files[i].getName());

65. }

66.

67. }

68.

69. }

java用itext怎么获取附件?

new FileInputStream("D:/word.doc") 很简单 这样就行了 Out是输出一个文档 In是读取一个文档进行编辑

查询附件列表java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于附件列表内容、查询附件列表java的信息别忘了在本站进行查找喔。

The End

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