I am new to Android Dev,This is my first Question in SO. i try to export my reports in Excel format. so i try to create the Excel file.Here is the code.
I am struck with get the json and put into the ROW and CELL
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView hwtxt = (TextView)findViewById(R.id.hw);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet firstSheet = workbook.createSheet("Sheet1");
String url = "http://192.168.1.13:8090/Sanjay_API/showBalanceAmtList.php";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("hwtxt", hwtxt.getText().toString()));
try {
JSONObject parentObject = new JSONObject(getJSONUrl(url, params));
JSONArray data = parentObject.getJSONArray("balance_list");
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String myVal1 = c.getString("entry_date");
System.out.println(myVal1);
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream fos = null;
try {
String str_path = Environment.getExternalStorageDirectory().toString();
File file;
//String CurrentDateAndTime = java.text.DateFormat.getDateTimeInstance().format(new Date());
file = new File(str_path, getString(R.string.app_name) + ".xls");
fos = new FileOutputStream(file);
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(MainActivity.this, "Excel Sheet Generated", Toast.LENGTH_SHORT).show();
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
And, I have a data in json which is return from APT using PHP, My json is
{
"vnumber": [{
"vid": "1",
"vnumber": "STAT01",
"vname": "STAT01",
"vyear": "2011",
"fcdate": "22/12/2017",
"insdate": "22/12/2017",
"perdate": "22/12/2017",
"gtax": "0123456",
"polcer": "TEST DEMO CERTIFY"
}, {
"vid": "2",
"vnumber": "STAT02",
"vname": "STAT02",
"vyear": "2015",
"fcdate": "12/12/2012",
"insdate": "12/12/2012",
"perdate": "12/12/2012",
"gtax": "100",
"polcer": "100"
}]
}
I dont know how to export my data in excel format in SD Card.