1
我遇到了excel表列的順序問題。我的計劃的樣品結果:如何使用Java按列單元格值對excel表進行排序?
PAGE | NAME
106 | 29100004-01
108 | 36110001-01
11 | 29100020-01
110 | 24000017-01
112 | 32430002-01
114 | 3831000201A
116 | 26150003-01
118 | 52210001-01
120 | 35100008-01
122 | 29100009-01
124 | 32400014-01
126 | 32330005-01
128 | 35100001-01
13 | 26234201-03
130 | 27514401-01
133 | 27510002-01
我用下面的源:
XSSFWorkbook workbook = new XSSFWorkbook();
// Create a blank sheet
XSSFSheet sheet = workbook.createSheet("WORKPACK DATA");
// This data needs to be written (Object[])
Map<String, Object[]> data = new TreeMap<String, Object[]>();
String strPreviousPage ="";
for (int i = 0; i < pdDoc.getNumberOfPages(); i++)
{
PDPage CurrentPage = (PDPage) allPages.get(i);
pdfStripper.extractRegions(CurrentPage);
int currentRow = i + 1;
Text = pdfStripper.getTextForRegion("KeyWordRegion");
if (Text.toLowerCase().contains(keyword.toLowerCase()))
{
TextCRI = pdfStripper.getTextForRegion("ExternalIdent");
String str = TextCRI.replace("\r\n", "");
String str2 = str.replace(" ", "");
if (str2.equalsIgnoreCase(strPreviousPage))
{
System.out.println("Keyword found on current page!");
}
else
{
System.out.println("Keyword found on current page!");
data.put(Integer.toString(currentRow), new Object[] {i + 1, str2 });
}
strPreviousPage = str2;
}
else
{
System.out.println("Keyword NOT found on current page!");
}
}
pdDoc.close();
// Iterate over data and write to sheet
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset)
{
Row row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr)
{
Cell cell = row.createCell(cellnum++);
if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Integer)
cell.setCellValue((Integer) obj);
}
}
try
{
// Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("C:\\INPUT\\STANIMIROV.xlsx"));
workbook.write(out);
out.close();
System.out.println("C:\\INPUT\\STANIMIROV.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
我怎樣才能在表中的數據進行排序?我需要PAGES
命令爲11,13,15 ......非10,101,12,125,13,131 ......
而不是像這樣做,用Java方式來做:創建一個類來保存每一行,實現「Comparable」,並使用'Collections.sort'。 – m0skit0