我现在写了一个很简单的将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, 15); lable = new Label(0, 3, "肚布ら戳", titleFormat); sheet.addCell(lable); lable = new Label(1, 3, "肚布絪腹", titleFormat); sheet.addCell(lable); lable = new Label(2, 3, "癸禜絪腹", titleFormat); sheet.addCell(lable); lable = new Label(3, 3, "癸禜?嘿", titleFormat); sheet.addCell(lable); lable = new Label(4, 3, "篕璶", titleFormat); sheet.addCell(lable); lable = new Label(5, 3, "?肂", titleFormat); sheet.addCell(lable); lable = new Label(6, 3, "?肂?璸", titleFormat); sheet.addCell(lable); ResultSet rs = DbUtil.exeQuery("exec dbo.Gl_EndDetail @Day = '" + date + "', @AcctNo1 = '" + sacct + "', @AcctNo2 = '" + eacct + "'"); float f1=0.0f; int k = 4; while (rs!=null&&rs.next()) { float nt_tot=rs.getFloat("nt_tot"); f1+=nt_tot; lable = new Label(0, k,Tool.deNull(rs.getString("voc_dat")),detFormat); sheet.addCell(lable); lable = new Label(1, k,Tool.deNull(rs.getString("voc_no")), detFormat); sheet.addCell(lable); lable = new Label(2, k,Tool.deNull(rs.getString("cus_no")), detFormat); sheet.addCell(lable); lable = new Label(3, k,Tool.deNull(rs.getString("cus_nam")), detFormat); sheet.addCell(lable); lable = new Label(4, k,Tool.deNull(rs.getString("memoa")), detFormat); sheet.addCell(lable); lable = new Label(5, k,Tool.changeCash(rs.getFloat("ntac_amt")), detFormat); sheet.addCell(lable); lable = new Label(6, k,Tool.changeCash(nt_tot), detFormat); sheet.addCell(lable); k++; } sheet.mergeCells(0, k + 1,6, 0); lable = new Label(0, k + 1, "?璸?肂? "+ Tool.changeCash(f1), detFormat); sheet.addCell(lable); book.write(); book.close(); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } } public void init() throws ServletException { }}