StringBuffer s 中保存有HTML 的TABLE 想通过POI 生成excel,直接用s.toString()去填充.是否可行?求代码

解决方案 »

  1.   

    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.hssf.record.*;
    import org.apache.poi.hssf.model.*;
    import org.apache.poi.hssf.util.*;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFRow; 
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
    import java.io.FileInputStream;
    public class xls2table
    {
    public static String fileToBeRead="e://test.xls";
    public static void main(String argv[])
    {
    try
    {
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
    HSSFSheet sheet = workbook.getSheet("Sheet1");
    int rows = sheet.getPhysicalNumberOfRows();
    for (int r = 0; r < rows; r++)
    {
    HSSFRow row = sheet.getRow(r);
    if (row != null)
    {
    int cells = row.getPhysicalNumberOfCells();
    String value = "";
    System.out.println(cells);
    for (short c = 0; c < cells; c++) 
    {
    HSSFCell cell = row.getCell(c);
    if (cell != null)
    {
    switch (cell.getCellType())
    {
    case HSSFCell.CELL_TYPE_FORMULA :
    //strCell = String.valueOf(aCell.getNumericCellValue());
    //returnstr+=strCell+" ";
    break;
    case HSSFCell.CELL_TYPE_NUMERIC:
    value += (long)cell.getNumericCellValue()+"t";
    break;
    case HSSFCell.CELL_TYPE_STRING:
    value += cell.getStringCellValue()+"t";
    break;
    case HSSFCell.CELL_TYPE_BLANK://blank
    //strCell = aCell.getStringCellValue();
    //returnstr+=strCell+" ";
    break;
    default:
    value +="t";
    }
    }
    }
    //下面可以将查找到的行内容用SQL语句INSERT到oracle
    System.out.println(value);
    //
    }
    }
    }catch(Exception e)
    {System.out.println(e);}
    }
    }
      

  2.   

    新建一个空白文件,改名为test.xls
    然后把以下内容拷贝进去
    再用Excel打开
    <table>
    <tr>
    <td>
    123
    </td>
    <td>
    234
    </td>
    </tr>
    <tr>
    <td>
    aaa
    </td>
    <td>
    bbb
    </td>
    </tr>
    </table>
      

  3.   

    其实Excel支持直接打开Html标签当然,如果你要进行聚合运算 就要另外想办法了
      

  4.   

    2楼 ???有误
     我是想用含有TABLE 的 String 生成EXCEL
      

  5.   

    还是有问题啊,wps 能查看,offic 2003 却是乱码