доработки по граблям, на этот раз поставлено ограничение в процессоре до 10 пустых строк, потому как иначе, на нек. шитах, кол-во строк может оказаться 65тыс. (при реальных 85)
Код:
sheet.getLastRowNum()
Познакомьтесь с пентестом веб-приложений на практике в нашем новом бесплатном курсе
доработки по граблям, на этот раз поставлено ограничение в процессоре до 10 пустых строк, потому как иначе, на нек. шитах, кол-во строк может оказаться 65тыс. (при реальных 85)
sheet.getLastRowNum()
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.util.*;
/**
* Created by mike on 28.07.14.
*/
public class POI2CSV {
private static final String encoding="UTF-8";
private final Map<String,List<Integer>> arrPathCSV = new HashMap<String,List<Integer>>();
private final Map<String,List<Integer>> arrPathMap = new HashMap<String,List<Integer>>();
private final int BLANK_LIM=10;//limit for blank rows, skip other data (stop additions to corresponding List)
private class CSVmap {
private BufferedWriter osCSV;
private BufferedWriter osMAP;
private final File tempcsv;
private final File tempmap;
public CSVmap(String xName) throws IOException {
xName=xName.replaceAll("\"","");
tempcsv = File.createTempFile(xName, ".csv");
tempmap = File.createTempFile(xName, ".map");
osCSV= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempcsv),encoding));
osMAP= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempmap), encoding));
}
public void close() throws IOException {
osCSV.close();
osMAP.close();
}
public void write(String cellReference, String formattedValue) throws IOException {
// System.out.println(cellReference+SEP);
char SEP = ';';
osMAP.write(cellReference + SEP);
osCSV.write(formattedValue + SEP);
}
public void newLine() throws IOException {
osMAP.newLine();
osCSV.newLine();
}
public String getTempcsv() {
return tempcsv.getAbsolutePath();
}
public String getTempmap() {
return tempmap.getAbsolutePath();
}
}
public void parseExcel(File file) throws IOException {
OPCPackage container;
try {
container = OPCPackage.open(file.getAbsolutePath());
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container);
XSSFReader xssfReader = new XSSFReader(container);
StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
while (iter.hasNext()) {
InputStream stream = iter.next();
//file.getName()+'_'+iter.getSheetName()
CSVmap csvmap=new CSVmap(file.getName()+'_'+iter.getSheetName());
processSheet(styles, strings, stream, csvmap);
stream.close();
csvmap.close();
}
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
}
}
protected void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream, final CSVmap csvmap) throws IOException, SAXException {
//duplicate for size in newLine
String lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
//length in bytes for default encoding, suppose UTF-8
final int lnsepLen=lineSeparator.getBytes(encoding).length;
final int sepLen=1;
InputSource sheetSource = new InputSource(sheetInputStream);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
final List <Integer> arrmap=new ArrayList<Integer>();
final List<Integer> arrdata=new ArrayList<Integer>();
arrPathMap.put(csvmap.getTempmap(), arrmap);arrPathCSV.put(csvmap.getTempcsv(), arrdata);
try {
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader sheetParser = saxParser.getXMLReader();
ContentHandler handler = new XSSFSheetXMLHandler(styles, strings, new XSSFSheetXMLHandler.SheetContentsHandler() {
int idx =0;//only one index for arrmap, arrdata
int blankcnt=0;//see BLANK_CNT
int lenmap=0, lendata =0;
public void startRow(int rowNum) {
if (blankcnt>BLANK_LIM) return;
arrmap.add(0);arrdata.add(0);
idx =arrmap.size()-1;
}
public void endRow() {
try {
if (blankcnt>BLANK_LIM) return;
csvmap.newLine();
if (arrmap.get(idx)<=sepLen) {
blankcnt++;}
else{blankcnt=0;} //check only one array (as array sizes are equal)
lenmap+=arrmap.get(idx) + lnsepLen;
arrmap.set(idx, lenmap);
lendata+=arrdata.get(idx) + lnsepLen;
arrdata.set(idx, lendata);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cell(String cellReference, String formattedValue) {
if (blankcnt>BLANK_LIM) return;
try {
formattedValue=formattedValue.replaceAll("[\n\r]", "|");
csvmap.write(cellReference, formattedValue);
//set data length for arrays
int len=arrmap.get(idx) + cellReference.getBytes(encoding).length;
arrmap.set(idx,len + sepLen);
len=arrdata.get(idx) + formattedValue.getBytes(encoding).length;
arrdata.set(idx,len + sepLen);
} catch (IOException e) {
e.printStackTrace();
}
}
public void headerFooter(String text, boolean isHeader, String tagName) {
}
},
//new DataFormatter(new Locale("en","UK")),
false//means result instead of formula
);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
// System.out.println("arrmap size:" + arrmap.size()+";->" +csvmap.getTempmap());
} catch (ParserConfigurationException e) {
throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());}
}
public Integer[] getArrMap(String key){
/* int[] ret= new int[arrmap.size()];
int i = 0;
for (Integer e : arrmap)
ret[i++] = e;
return ret;
*/
List<Integer> arr=arrPathMap.get(key);
System.out.println("map index size:" + arr.size());
return arr.toArray(new Integer[0]);
}
public Integer[] getArrData(String key){
List <Integer> arr=arrPathCSV.get(key);
System.out.println("csv index size:"+arr.size());
return arr.toArray(new Integer[0]);
}
public String[] getArrPathCSV() {
Set<String> keys=arrPathCSV.keySet();
List<String> forsort=new ArrayList<String>(keys);
java.util.Collections.sort(forsort);
return forsort.toArray(new String[0]);
}
public String[] getArrPathMap() {
Set<String> keys=arrPathMap.keySet();
List<String> forsort=new ArrayList<String>(keys);
java.util.Collections.sort(forsort);
return forsort.toArray(new String[0]);
}
public static void main(String args[]){
POI2CSV inst=new POI2CSV();
try {
System.out.println(args[0]);
inst.parseExcel(new File(args[0]));
String[] mappath=inst.getArrPathMap();
String[] csvpath=inst.getArrPathCSV();
int idx=0;
Integer[] testmap=inst.getArrMap(mappath[idx]);Integer[] test=inst.getArrData(csvpath[idx]);
System.out.println(csvpath[idx]);
System.out.println(mappath[idx]+"; array size:" + mappath.length);
System.out.println(test[3] + "; array size:" + test.length);
System.out.println(testmap[3] + "; array size:" + testmap.length);
RandomAccessFile raf = new RandomAccessFile(mappath[idx], "r");
raf.seek(testmap[3]);
System.out.println("line #5 -->" + raf.readLine());
raf.close();
raf=new RandomAccessFile(csvpath[idx], "r");
raf.seek(test[3]);
FileDescriptor fd = raf.getFD();
FileReader fr = new FileReader(fd);
BufferedReader br = new BufferedReader(fr);
System.out.println("csv line #5 -->" +br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
lenmap+=arrmap.get(idx) + lnsepLen;
arrmap.set(idx, lenmap);
lendata+=arrdata.get(idx) + lnsepLen;
arrdata.set(idx, lendata);
'если строка и дата содержит разделители /
'предполагаем формат США mm/dd/yy
'при неуказанном формате даты Excel POI SAX выведеn строку в таком формате
If DataType(v)=V_STRING Then
Dim arr:arr=Split(v, DAT_SEP_USA)
If UBound(arr)<>2 Then
Error 1024, CM_WRONGTYPE4DAT _
&{at idx:} &CStr(idx) &{;val:} &CStr(v)
End If
v=DateNumber(arr(2),arr(0),arr(1))
End If
package org.lmike;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class NotesDocument {
@JsonProperty("UniversalID")
String unid;
@JsonProperty("Items")
JsonNode items;
}
package org.lmike;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class MyColumns <T> {
List<T> lst=new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String name;
public T get(int index) {
return lst.get(index);
}
public int size() {
return lst.size();
}
public void add(T t) {
lst.add(t);
}
}
public List<NotesDocument> unmarshal(InputStream in) throws IOException {
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
.serializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.build();
//https://stackoverflow.com/a/6349488
return mapper.readValue(in,
mapper.getTypeFactory().constructCollectionType(List.class, NotesDocument.class));
}
public static void main(String[] args) throws IOException {
....
List<NotesDocument> notesDocuments=obj.unmarshal(in);
....
Iterator<Map.Entry<String, JsonNode>> iterator=notesDocuments.get(0).items.fields();
List<List<String>> table = new ArrayList<>();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> entry = iterator.next();
if (entry.getKey().toLowerCase().startsWith("name_")) {
LOG.debug(entry.getKey() + ":" + entry.getValue());
List<String> values=StreamSupport
.stream(entry.getValue().get("Values").spliterator(), false)
.map(JsonNode::asText)
.collect(Collectors.toList());
table.add(values);
}
}
List<MyColumns<String>> tableValues=myTranspose(table);
context.putVar("tableValues", tableValues);
Обучение наступательной кибербезопасности в игровой форме. Начать игру!