日期:2014-05-19  浏览次数:20689 次

JAVA将txt转化为excel遇到的两个问题
我现在写了一个很简单的将txt转化为excel的JAVA程序.
txt是很规则的一个文件
类似于:姓名  年龄  性别 工资 身高
    AA  22  女  3000 175
而导进去的excel表格有一个写好的表头,大楷8行!
是设置好了的格式
现在我只需要从excel表格第9行开始写入txt文挡中的内容
整个代码不足100行
但是遇到两个问题:
一:汉字导进去是乱码
二:不知道如何定位到excel表格的第9行
我用的是POI操作excel   读txt用的是BufferedReader readline 方法 
通过令牌化字符串  将内容填到excel中去
出现乱码后我也设置了excel表格的编码格式为:UTF_16
可是问题依然没得到解决
是不是BufferedReader不能设置编码格式问题 那不通过该类还有别的读方法能将内容分割读进excel表格吗?
小弟JAVA菜鸟
那位高人能指点一下迷径,感激不尽!


------解决方案--------------------
试试这个!把数据库取数据的换成你读取的信息!还需要jxl.jar包
Java code

public class ExportExcelKmye extends HttpServlet {

    public ExportExcelKmye() {
        super();
    }

    public void destroy() {
        super.destroy(); 
        
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            String exceldate = Tool.getActicleTime().substring(0, 10);
            String sacct = request.getParameter("sacct");
            String eacct = request.getParameter("eacct");
            String date = request.getParameter("date");
            String state = request.getParameter("state");

            response.setContentType("APPLICATION/msexcel;charset=BIG5");
            response
                    .setHeader("Content-Disposition", "attachment; filename="
                            + new String("ヘ緇肂灿.xls".getBytes("BIG5"),
                                    "ISO8859_1"));
            OutputStream os = null;
            try {
                os = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            WritableWorkbook book = Workbook.createWorkbook(os);
            WritableSheet sheet = book.createSheet("材", 0);
            WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 15,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.GREEN);
            WritableCellFormat headerFormat = new WritableCellFormat(headerFont);

            WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10,
                    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLACK);
            WritableCellFormat titleFormat = new WritableCellFormat(titleFont);

            WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10,
                    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLACK);
            WritableCellFormat detFormat = new WritableCellFormat(detFont);

            sheet.mergeCells(0, 0, 13, 0);// 虫じ
            Label lable = new Label(
                    0,
                    0,
                    "                                                               ヘ緇肂灿 ",
                    headerFormat);
            sheet.addCell(lable);
            sheet.mergeCells(0, 1, 11, 0);
            lable = new Label(0, 1, "ら戳:" + exceldate);
            sheet.addCell(lable);

            // ︽糴砞竚
            sheet.setColumnView(0, 15);
            sheet.setColumnView(1, 15);
            sheet.setColumnView(2, 12);
            sheet.setColumnView(3, 15);
            sheet.setColumnView(4, 35);
            sheet.setColumnView(5, 12);
            sheet.setColumnView(6, 15);
            sheet.setColumnView(7,