-
Notifications
You must be signed in to change notification settings - Fork 321
improving format of exported excel #1456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.io.OutputStreamWriter; | ||
| import java.math.BigDecimal; | ||
| import java.util.Calendar; | ||
|
|
||
| /** | ||
| * @author akurutin on 04.04.2017. | ||
|
|
@@ -53,4 +55,35 @@ public void close() throws IOException { | |
| myCsvPrinter.flush(); | ||
| myCsvPrinter.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public void newSheet() throws IOException { | ||
| println(); | ||
| println(); | ||
| } | ||
|
|
||
| @Override | ||
| public void newSheet(String name) throws IOException { | ||
| newSheet(); | ||
| } | ||
|
|
||
| @Override | ||
| public void print(Double value) throws IOException { | ||
| myCsvPrinter.print(String.valueOf(value)); | ||
| } | ||
|
|
||
| @Override | ||
| public void print(Integer value) throws IOException { | ||
| myCsvPrinter.print(String.valueOf(value)); | ||
| } | ||
|
|
||
| @Override | ||
| public void print(BigDecimal value) throws IOException { | ||
| myCsvPrinter.print(value.toPlainString()); | ||
| } | ||
|
|
||
| @Override | ||
| public void print(Calendar value) throws IOException { | ||
| myCsvPrinter.print(value.toString()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this can also be replaced with |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,13 @@ | |
| import com.google.common.collect.Iterables; | ||
| import com.google.common.collect.Iterators; | ||
| import com.google.common.collect.Lists; | ||
|
|
||
| import net.sourceforge.ganttproject.language.GanttLanguage; | ||
|
|
||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
| import org.apache.poi.ss.usermodel.Cell; | ||
| import org.apache.poi.hssf.usermodel.HSSFDateUtil; | ||
| import org.apache.poi.ss.usermodel.Row; | ||
| import org.apache.poi.ss.usermodel.Sheet; | ||
| import org.apache.poi.ss.usermodel.Workbook; | ||
|
|
||
| import java.io.IOException; | ||
|
|
@@ -32,6 +36,7 @@ | |
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.text.SimpleDateFormat; | ||
|
|
||
| /** | ||
| * @author torkhov | ||
|
|
@@ -40,10 +45,12 @@ class XlsReaderImpl implements SpreadsheetReader { | |
|
|
||
| private final Workbook myBook; | ||
| private final Map<String, Integer> myHeaders; | ||
| private final SimpleDateFormat myDateFormat; | ||
|
|
||
| XlsReaderImpl(InputStream is, List<String> columnHeaders) throws IOException { | ||
| myBook = new HSSFWorkbook(is); | ||
| myHeaders = initializeHeader(columnHeaders); | ||
| myDateFormat = GanttLanguage.getInstance().getShortDateFormat(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -53,11 +60,50 @@ public void close() throws IOException { | |
|
|
||
| @Override | ||
| public Iterator<SpreadsheetRecord> iterator() { | ||
| return Iterators.transform(myBook.getSheetAt(0).iterator(), (input) -> new XlsRecordImpl(getCellValues(input), myHeaders)); | ||
| Iterator<SpreadsheetRecord> iterator = null; | ||
|
|
||
| if(myBook.getNumberOfSheets() > 1) { | ||
| Iterator<SpreadsheetRecord> it = null; | ||
| for (int i = 0; i < myBook.getNumberOfSheets(); ++i) { | ||
| Sheet sheet = myBook.getSheetAt(i); | ||
| if (i < myBook.getNumberOfSheets() - 1) { | ||
| int lastRow = sheet.getPhysicalNumberOfRows(); | ||
| sheet.createRow(lastRow + 1); | ||
|
||
| sheet.createRow(lastRow + 2); | ||
| } | ||
| it = Iterators.transform(sheet.iterator(), (input) -> new XlsRecordImpl(getCellValues(input), myHeaders)); | ||
| iterator = (iterator == null) ? it : Iterators.concat(iterator, it); | ||
| } | ||
| } | ||
| else { | ||
|
||
| iterator = Iterators.transform(myBook.getSheetAt(0).iterator(), (input) -> new XlsRecordImpl(getCellValues(input), myHeaders)); | ||
| } | ||
|
|
||
| return iterator; | ||
| } | ||
|
|
||
| private List<String> getCellValues(Row row) { | ||
| return Lists.newArrayList(Iterables.transform(row, Cell::getStringCellValue)); | ||
| return Lists.newArrayList(Iterables.transform(row, | ||
| (input) -> { | ||
|
||
| switch (input.getCellTypeEnum()) { | ||
| case NUMERIC: { | ||
| if (HSSFDateUtil.isCellDateFormatted(input)) { | ||
| return myDateFormat.format(input.getDateCellValue()); | ||
| } | ||
| else { | ||
| if (input.getCellStyle().getDataFormat() == (short)1) { | ||
|
||
| return String.valueOf((int)input.getNumericCellValue()); | ||
| } | ||
| else { | ||
| return String.valueOf(input.getNumericCellValue()); | ||
| } | ||
| } | ||
| } | ||
| default: | ||
| return input.getStringCellValue(); | ||
| } | ||
| } | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ public String get(String name) { | |
| if (index == null) { | ||
| throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name, myMapping.keySet())); | ||
| } | ||
| return myValues.get(index); | ||
| return (myValues.size() <= index) ? new String() : myValues.get(index); | ||
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just return
""instead ofnew String()