XLS export almost done

fix/get-all-stations-hangout
Mateusz Lubecki 2021-10-24 11:28:48 +02:00
rodzic 7577673df6
commit 42256bf457
9 zmienionych plików z 189 dodań i 29 usunięć

Wyświetl plik

@ -3,6 +3,8 @@ package cc.pogoda.mobile.pogodacc.activity;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.UriPermission;
import android.database.Cursor;
@ -59,13 +61,13 @@ public class ExportDataActivity extends AppCompatActivity {
WeatherStation stationToExport = null;
Uri exportUri;
Uri exportUri = null;
public WeatherStation getStationToExport() {
return stationToExport;
}
public void openDirectory(Uri uriToLoad) {
public void openDirectory(String fn, int outputFormat) {
// // Choose a directory using the system's file picker.
// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
//
@ -77,12 +79,15 @@ public class ExportDataActivity extends AppCompatActivity {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/vnd.ms-excel");
intent.putExtra(Intent.EXTRA_TITLE, "text.xls");
if (outputFormat == 1) {
intent.setType("text/csv");
intent.putExtra(Intent.EXTRA_TITLE, fn + ".csv");
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when your app creates the document.
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
}
if (outputFormat == 2) {
intent.setType("application/vnd.ms-excel");
intent.putExtra(Intent.EXTRA_TITLE, fn + ".xls");
}
startActivityForResult(intent, 123);
@ -194,7 +199,17 @@ public class ExportDataActivity extends AppCompatActivity {
outputFileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openDirectory(null);
String fn;
if (stationToExport != null) {
fn = stationToExport.getSystemName() + "_" + datePicker.getDayOfMonth() + String.format("%02d", datePicker.getMonth()) + "_ln_" + act.getExportLnInHours() + "hrs";
}
else {
fn = "export";
}
openDirectory(fn, act.getOutputFormat());
}
});
@ -223,7 +238,7 @@ public class ExportDataActivity extends AppCompatActivity {
WeatherStation toExport = act.getStationToExport();
if (toExport != null) {
if (toExport != null && exportUri != null) {
long timestampStart = act.getStartTimestamp();
long timestampStop = timestampStart + act.getExportLnInHours() * 3600;
@ -244,6 +259,15 @@ public class ExportDataActivity extends AppCompatActivity {
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(act);
builder.setMessage(R.string.select_station_export);
builder.setPositiveButton(R.string.ok, (DialogInterface var1, int var2) -> {
var1.dismiss();
});
builder.create();
builder.show();
}
}
});

Wyświetl plik

@ -94,6 +94,7 @@ public class MainActivity extends AppCompatActivity {
fromFavs.setLat(fromAllStations.getLat());
fromFavs.setDisplayedName(fromAllStations.getDisplayedName());
fromFavs.setDisplayedLocation(fromAllStations.getDisplayedLocation());
fromFavs.setTimezone(fromAllStations.getTimezone());
// there is no need to delete and put object on the list once again
// as a list does not make a copy of the object. It (ArrayList) keeps

Wyświetl plik

@ -55,20 +55,8 @@ public class TrendActivity extends AppCompatActivity {
NavDirections wind = WindTrendFragmentDirections.actionNavigationWindToNavigationTemperature(stationName);
NavDirections pressure = PressureTrendFragmentDirections.actionNavigationPressureToNavigationWind(stationName);
//NavHostFragment.create(R.navigation.mobile_navigation, bundle);
// NavGraph navGraph = navController.getNavInflater().inflate(R.navigation.mobile_navigation);
// builder.setDefaultValue(stationName);
// navGraph.addArgument("station", builder.build());
// navController.setGraph(navGraph, bundle);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
//navController.navigate(temperature);
//
// NavArgument.Builder builder = new NavArgument.Builder();
// builder.setDefaultValue(stationName);
// navGraph.addArgument("station", builder.build());
// navController.setGraph(navGraph);
}

Wyświetl plik

@ -66,6 +66,7 @@ public class AllStationsDao {
elem.setStationNameTextColor(def.stationNameTextColour);
elem.setImageAlign(def.backgroundJpgAlign);
elem.setMoreInfo(def.moreInfo);
elem.setTimezone(def.timezone);
AvailableParameters availableParameters = AvailableParameters.fromStation(def);
elem.setAvailableParameters(availableParameters);

Wyświetl plik

@ -7,21 +7,40 @@ import android.provider.DocumentsContract;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.threeten.bp.Instant;
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import cc.pogoda.mobile.pogodacc.type.WeatherStation;
import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData;
import cc.pogoda.mobile.pogodacc.type.web.StationData;
public class ExcelExport {
private static final SimpleDateFormat SDF = new SimpleDateFormat("dd-M-yyyy HH:mm:ss");
private static double round (double in) {
double out = 0;
long temp = (long)(in * 10);
out = temp / 10.0d;
return out;
}
public static boolean exportToExcel(ListOfStationData data, WeatherStation station, Context context, OutputStream out) {
@ -29,38 +48,152 @@ public class ExcelExport {
return false;
}
if (data == null || data.list_of_station_data.length == 0) {
return false;
}
int rowNumber = 0;
Cell cell;
ZonedDateTime first = ZonedDateTime.ofInstant(Instant.ofEpochSecond(data.list_of_station_data[0].epoch), ZoneId.of(station.getTimezone()));
String generalTimezone = first.format(DateTimeFormatter.ofPattern("zzzz"));
Workbook workbook = new HSSFWorkbook();
CellStyle left = workbook.createCellStyle();
left.setAlignment(CellStyle.ALIGN_LEFT);
Sheet sheet = workbook.createSheet(station.getSystemName());
Row header = sheet.createRow(3);
{
Row stationame = sheet.createRow(rowNumber++);
Cell name = stationame.createCell(0);
name.setCellValue("Station name:");
name = stationame.createCell(1);
name.setCellValue(station.getDisplayedName());
Row localization = sheet.createRow(rowNumber++);
Cell loc = localization.createCell(0);
loc.setCellValue("Location:");
loc = localization.createCell(1);
loc.setCellValue(station.getDisplayedLocation());
Row coordinates = sheet.createRow(rowNumber++);
Cell coord = coordinates.createCell(0);
coord.setCellValue("Coordinates:");
coord = coordinates.createCell(1);
coord.setCellValue("Lat " + station.getLat() + " , Lon " + station.getLon());
Row timezone = sheet.createRow(rowNumber++);
Cell tz = timezone.createCell(0);
tz.setCellValue("All date and time as in:");
tz = timezone.createCell(1);
tz.setCellValue(station.getTimezone());
Row generaltz = sheet.createRow(rowNumber++);
Cell gtz = generaltz.createCell(0);
gtz.setCellValue("Timezone in this export:");
gtz = generaltz.createCell(1);
gtz.setCellValue(generalTimezone);
Row offset = sheet.createRow(rowNumber++);
Cell off = offset.createCell(0);
off.setCellValue("Time offset for the timezone:");
off = offset.createCell(1);
off.setCellValue(first.getOffset().toString());
// Row aprscall = sheet.createRow(rowNumber++);
// Cell call = aprscall.createCell(0);
// call.setCellValue("APRS Callsign:");
// call = aprscall.createCell(1);
// call.setCellValue(station.get);
}
rowNumber++;
rowNumber++;
rowNumber++;
Row header = sheet.createRow(rowNumber++);
cell = header.createCell(0);
cell.setCellValue("UNIX Epoch Timestamp");
cell.setCellStyle(left);
cell = header.createCell(1);
cell.setCellValue("Date / Time");
cell.setCellValue("Date / Time [DD-MM-YYYY 24HH:MM]");
cell.setCellStyle(left);
cell = header.createCell(2);
cell.setCellValue("Temperature");
cell.setCellValue("Temperature [C]");
cell.setCellStyle(left);
cell = header.createCell(3);
cell.setCellValue("QNH pressure");
cell.setCellValue("QNH pressure [hPa]");
cell.setCellStyle(left);
cell = header.createCell(4);
cell.setCellValue("Humidity");
cell.setCellValue("Humidity [%]");
cell.setCellStyle(left);
cell = header.createCell(5);
cell.setCellValue("Wind Direction");
cell.setCellStyle(left);
cell = header.createCell(6);
cell.setCellValue("Wind Average Speed");
cell.setCellValue("Wind Average Speed [m/s]");
cell.setCellStyle(left);
cell = header.createCell(7);
cell.setCellValue("Wind Gusts");
cell.setCellValue("Wind Gusts [m/s]");
cell.setCellStyle(left);
// put data into output file
for (StationData d : data.list_of_station_data) {
Row r = sheet.createRow(rowNumber++);
Cell epoch = r.createCell(0);
epoch.setCellValue(d.epoch);
epoch.setCellStyle(left);
ZonedDateTime zoneddatetime = ZonedDateTime.ofInstant(Instant.ofEpochSecond(d.epoch), ZoneId.of(station.getTimezone()));
Cell datetime = r.createCell(1);
datetime.setCellValue(zoneddatetime.format(DateTimeFormatter.ofPattern("dd-M-yyyy HH:mm:ss")));
datetime.setCellStyle(left);
Cell temperature = r.createCell(2);
temperature.setCellValue(round(d.temperature));
temperature.setCellStyle(left);
Cell pressure = r.createCell(3);
pressure.setCellValue(d.pressure);
pressure.setCellStyle(left);
Cell humidity = r.createCell(4);
humidity.setCellValue(d.humidity);
humidity.setCellStyle(left);
Cell winddir = r.createCell(5);
winddir.setCellValue(d.winddir);
winddir.setCellStyle(left);
Cell windspeed = r.createCell(6);
windspeed.setCellValue(round(d.windspeed));
windspeed.setCellStyle(left);
Cell windgust = r.createCell(7);
windgust.setCellValue(round(d.windgusts));
windgust.setCellStyle(left);
}
sheet.setColumnWidth(0, 6600);
sheet.setColumnWidth(1, 5900);
sheet.setColumnWidth(2, 3900);
sheet.setColumnWidth(3, 4600);
sheet.setColumnWidth(4, 2900);
sheet.setColumnWidth(5, 3200);
sheet.setColumnWidth(6, 3700);
sheet.setColumnWidth(7, 3200);
try {
workbook.write(out);

Wyświetl plik

@ -56,6 +56,7 @@ public class ParceableStationsList implements Parcelable {
parcel.writeString(s.getDisplayedLocation());
parcel.writeString(s.getSponsorUrl());
parcel.writeString(s.getImageUrl());
parcel.writeString(s.getTimezone());
parcel.writeInt(s.getImageAlign());
parcel.writeInt(s.getStationNameTextColor());
@ -90,6 +91,7 @@ public class ParceableStationsList implements Parcelable {
wx.displayedLocation = in.readString();
wx.sponsorUrl = in.readString();
wx.imageUrl = in.readString();
wx.timezone = in.readString();
wx.imageAlign = in.readInt();
wx.stationNameTextColor = in.readInt();

Wyświetl plik

@ -130,6 +130,16 @@ public class WeatherStation implements Serializable {
String imageUrl;
public String getTimezone() {
return timezone.trim();
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
String timezone;
int imageAlign;
int stationNameTextColor;

Wyświetl plik

@ -26,6 +26,8 @@ public class StationDefinition {
public String moreInfo;
public String timezone;
public float lat;
public float lon;

Wyświetl plik

@ -123,7 +123,6 @@
<string name="csv" translatable="false">CSV</string>
<string name="xlsx" translatable="false">MS Excel XLS</string>
<string-array name="export_formats">
<item>@string/email</item>
<item>@string/csv</item>
<item>@string/xlsx</item>
</string-array>