Help me please. There is a program that I have not completed. The goal of the task is to create a program in java so that it transfers the html table to excel. I will be grateful.

import java.awt.Color; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import org.apache.poi.hslf.model.AutoShape; import org.apache.poi.hslf.model.Line; import org.apache.poi.hslf.model.ShapeTypes; import org.apache.poi.hslf.model.Slide; import org.apache.poi.hslf.model.Table; import org.apache.poi.hslf.model.TableCell; import org.apache.poi.hslf.model.TextBox; import org.apache.poi.hslf.usermodel.RichTextRun; import org.apache.poi.hslf.usermodel.SlideShow; import org.apache.poi.hssf.usermodel.HSSFCell; 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.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.StringUtil; public class Samplefile { public static void main(String args[]) throws Exception{ System.out.println("test"); File fl=new File("test.html"); FileInputStream finp=new FileInputStream(fl); InputStreamReader isr = new InputStreamReader(finp); BufferedReader buff = new BufferedReader( isr ); StringBuffer strBuff = new StringBuffer(); ArrayList<String> arrstr = new ArrayList<String>(); //читаем строку, а не чар String s; while ( ( s = buff.readLine() ) != null ) { strBuff.append(s); arrstr.add(s); } String str= strBuff.toString(); Workbook wb = new HSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet1 = wb.createSheet("test"); Row row = sheet1.createRow((short)0); // Create a cell and put a value in it. Cell cell = row.createCell(0); cell.setCellValue(1); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat( createHelper.createDataFormat().getFormat("dd.mm.yyyy")); // Or do it on one line. row.createCell(1).setCellValue(1.2); row.createCell(2).setCellValue( createHelper.createRichTextString("This is a string")); row.createCell(3).setCellValue(true); row.createCell(4).setCellValue("ddd"); cell=row.createCell(5); cell.setCellValue(Calendar.getInstance()); cell.setCellStyle(cellStyle); cell=row.createCell(6); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); cell = row.createCell(7,HSSFCell.CELL_TYPE_ERROR); String file="test.xls"; FileOutputStream outfl = new FileOutputStream(file); wb.write(outfl); outfl.close(); } } 
  • 3
    What you can not do? - yozh
  • It’s impossible to write a loop to read exactly what is between the <td></td> tags, for example. - art
  • one
    Parser needed. - Gorets
  • I'm new to this one. I can not say for sure if I know it. I have this task and I need to do it as quickly as possible. If you can help with anything, help, please ... - art
  • there are parsers like DOM, SAX and so on, if you don’t have time to figure them out, write a primitive parser yourself, like "if char = <td>, then write words to the buffer until there are charms = </ td>, at the end output buffer to file " - Gorets

1 answer 1

 // Читаем строку, а не чар String s; while ( s = buff.readLine() != null ) { if(s.trim() = "<td>") { while ( s = buff.readLine() != "</td>") { strBuff.append(s); arrstr.add(s); } } } 

Perhaps something like that =)

  • Which mistakes? - Gorets
  • one
    @Gorets, if (s.trim () = "<td>") Which language is it from? Do you think this is a comparison in Java? @ Art, compare the read line (removing spaces at the beginning and end) with another line: if (s.trim (). equals ("<td>")) {...} But! Why did you decide that there is nothing in addition to the tag? A bold assumption. - avp
  • I still watch and think that he is cutting his eyes - Gorets
  • You (@art), instead of your program, it is better to provide a sample of the data you want to convert. Everyone is lazy about the general case of html to parse. Tips in this regard you have already written. And what OS and why Java? - avp
  • one
    It was then ( take any html table ), as they say, and the dog rummaged ... Ie You do not have a one-time data transfer, but an educational task ? - avp