简体   繁体   中英

Save HTML and read in Android

I have created a function to generate receipt to save as text in the file. but when I try to read that file, it shows me with HTML tags. but when I open the text file from a phone using WPS office that doesn't show me HTML tags this is when I open text file in phone:

在此处输入图片说明

this when I read file: 在此处输入图片说明

and this my code-:

        File file = new File(Environment.getExternalStorageDirectory().toString());
    file.mkdir();

    if (file != null) {
        Log.d("created", "notnull");
    } else {
        Log.d("created", "null");
    }

    File gpxfile = new File(file, "samples.txt");
    FileWriter writer = null;
    try {
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxfile), Charset.forName("ASCII")));
        //writer = new FileWriter(gpxfile);
        //writer.write(billHTML());
        out.write(billHTML());
        //Log.d("writer", "" + billHTML());
        //writer.close();
        out.close();

       **here i read file** 
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(gpxfile), "ASCII"));
        String sData;
        while ((sData = in.readLine()) != null) {
            System.out.println(sData);
        }
        Log.d("files", "" + in);

    } catch (IOException e) {
        e.printStackTrace();
    }

this is genreate bill function.

    public String billHTML() {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");

    Calendar c = Calendar.getInstance();

    StringBuilder sb = new StringBuilder();
    sb.append("<html>"
            + "<body>");
    sb.append("<br>");
    sb.append("<h1><br><center> Texto mi marca </<center></h1>");
    sb.append("<h2><center> Texto mi marca </<center></h2>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<center>Fecha:  &emsp;" + sdf.format(c.getTime()));
    sb.append("<br>");
    sb.append("<center>Factura:  &emsp;" + sdf.format(c.getTime()));
    sb.append("<br>");
    sb.append("<center>Cllente al contado");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<h2><center>-------------------------------------------</h2>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<table width=550>" +
            "  <tr>" +
            "    <th  width='50%' align='left'> Description</th>" +
            "    <th  width='10%' align='left'>Units</th>" +
            "    <th  width='10%' align='right'>Price</th>" +
            "  </tr>");
    for (int i = 0; i < Util.itemArrayList.size(); i++) {
        sb.append("<tr>"
                + "<td>").append(Util.itemArrayList.get(i).description)
                .append("</td>"
                        + "<td align='left'>")
                .append(Util.itemArrayList.get(i).units)
                .append("</td>"
                        + "<td align='right'>")
                .append(Util.itemArrayList.get(i).price + "€")
                .append("</td>"
                        + "</tr>");
        oldpay = Util.itemArrayList.get(i).price;
        totalprice = totalprice + oldpay;
    }
    float basevalue = (float) (totalprice / 1.21);
    float ivaValue = totalprice - basevalue;
    sb.append("<tr>"
            + "<td>").append("-----------------------")
            .append("</td>"
                    + "<td align='right'>");
    sb.append("<tr>"
            + "<td>")
            .append("(Total)Base imponible")
            .append("</td>"
                    + "<td align='right'>")
            .append("</td>  "
                    + "<td align='right'>")
            .append(decimalFormat.format(basevalue) + "€")
            .append("</td>"
                    + "</tr>");

    sb.append("<tr>"
            + "<td>")
            .append("IVA (+21%)")
            .append("</td>"
                    + "<td align='right'>")
            .append("</td>  "
                    + "<td align='right'>")
            .append(decimalFormat.format(ivaValue) + "€")
            .append("</td>"
                    + "</tr>");

    sb.append("<tr>" +
            "<td>").append("==============================================").append("</td>" +
            "<td>==========</td>" +
            "<td>===========</td>");

    sb.append("<tr>"
            + "<td>").
            append("<b>Total </b>")
            .append("</td>"
                    + "<td align='right'>")
            .append("</td>  "
                    + "<td align='right'>")
            .append(decimalFormat.format(totalprice) + "€")
            .append("</td>"
                    + "</tr>");

    sb.append("</table>");
    System.out.print(sb.toString());


    return sb.toString();
}

If you are using html markup to generate the receipt you probably should name the file something like sample.html instead of sample.txt this way in your computer will be opened by a internet browser instead of the notepad.

Probably WPS Office is smart enough to detect the html tags and display the file with the correct formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM