Tag: java

文件下载时出错

在我的servlet中,我使用cookies传递文件path并下载文件如下 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String b = null; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("thecookie")) { b = cookie.getValue(); } } } BufferedReader br = new BufferedReader(new FileReader(b+"/logs.txt")); String path = br.readLine(); br.close(); […]

如何在使用apache poi阅读时在excel中获取冻结或拆分列的信息?

当我通过apache poi文档时,他们说你可以使用util包中的PanelInformation对象来获取冻结或拆分窗格的信息。 为此,我需要HSSFSheet类的对象。 我能够获得HSSFSHeet对象,但是当我试图从getPanelInformation方法获取PanelInformation。 它返回我空。 HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheetAt(sheetNumber); workbook.setActiveSheet(sheetNumber); System.out.println(sheet.getPaneInformation());

在两个javadate之间通过了不同的时区

我有两个date,但一个是CET,另一个是CEST。 Calendar calFrom = Calendar.getInstance(); calFrom.set( 2013, 2, 20,0,0,0); Calendar calTo = Calendar.getInstance(); calTo.set( 2013, 3, 5,0,0,0); long daysPassed= (calTo.getTime().getTime() – calFrom.getTime().getTime()) / (1000 * 60 * 60 * 24); System.out.println("calFrom: " + calFrom.getTime()); System.out.println("calTo: " + calTo.getTime()); System.out.println("daysPassed: " + daysPassed); 输出说: calFrom: Wed Mar 20 00:00:00 CET 2013 calTo: Fri Apr 05 00:00:00 […]

空指针exception将Jtable写入Excel

我试图从Jtable写入date到Excel。 这是我的公共无效做到这一点: public void toExcel(JTable table, File file){ try{ TableModel model = table.getModel(); FileWriter excel = new FileWriter(file); for(int i = 0; i < model.getColumnCount(); i++){ excel.write(model.getColumnName(i) + "\t"); } excel.write("\n"); for(int i=0; i< model.getRowCount(); i++) { for(int j=0; j < model.getColumnCount(); j++) { excel.write(model.getValueAt(i,j).toString()+"\t"); } excel.write("\n"); } excel.close(); }catch(IOException e){ System.out.println(e); } } 这是我让我的代码做到这一点: […]

如何在合并单元格poi java中插入值

我已经创build了新的行,然后合并两个单元格,然后我想获得这个合并单元格中的值 我的代码: sheet.createRow(1); HSSFRow row2 = sheet.getRow(1); HSSFCell cell = row2.getCell(1); cell.setCellValue("معلومات الطالب"); sheet.addMergedRegion(CellRangeAddress.valueOf("A2:B2")); 但我得到错误java.lang.NullPointerException

通过其内容获取单元格

有没有办法获取单元格对象或坐标单元格中​​包含的数据? 例如,如果坐标为(1; 5)的单元格包含string"FINDME" ,我想要做一些像Workbook.GetCellByData(“FINDME”),它应该返回Cell对象或(1; 5)。 我在Apache POI网站上find了一个可能有用的代码片段 。 我可以只读整个工作簿,并用IF语句查找数据,但是这样很脏… 编辑 :我已经编码的“脏”的解决scheme如下: public Cell getCellByContent(String data) { for (Row row : wb.getSheetAt(0)) { for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING){ System.out.println(String.format("Found String type at (%s,%s) and read: %s", row.getRowNum(), cell.getColumnIndex(), cell.getStringCellValue())); if (cell.getStringCellValue() == data) { //HERE return cell; //HERE } //HERE } […]

Excel表单格式化使用Apache POI

我正在从使用Apache POI库的Java代码创build一个Excel报告。 格式化Excel单元格时遇到问题。 请在下面find一段代码。 XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("coverageData"); int rownum = 0,cellnum=0; Row row1 = sheet.createRow(rownum++); Cell cell10 = row1.createCell(cellnum++); cell10.setCellValue("cell data"); XSSFCellStyle row1style = (XSSFCellStyle)cell10.getCellStyle(); XSSFColor grayColor = new XSSFColor(Color.DARK_GRAY); row1style.setFillBackgroundColor(grayColor); cell10.setCellStyle(row1style); try { //Write the workbook in file system FileOutputStream out = new FileOutputStream(new File("P:\\automation\\Spreadsheet.xlsx")); workbook.write(out); out.close(); System.out.println("Spreadsheet.xlsx […]

如何将Exceldate转换为Java的Timestamp?

我正在接收有人在Excel中写的CSV文件,我需要parsing这些文件到我的数据库。 然而,当我试图parsingdate到一个时间戳,它告诉我的格式是不正确的,并期望YYYY-MM-DD hh:mm:ss 有两种可能的解决办法,我不知道哪一个是可行的。 1.在导出csv之前,先将date转换为YYYY-MM-DD hh:mm:ss 2.以某种方式转换Java中的Exceldate格式。 我试图改变在Excel中的格式,但我没有看到任何格式化选项导致YYYY-MM-DD hh:mm:ss我得到最接近的是YY-MMM-DD或MM / DD / YY 哪一个是更好的溶剂,1或2,我该怎么做? 编辑相关的Java代码片段: public static List<AffiliateEntry> convert(ArrayList<String[]> collection) { List<AffiliateEntry> entryList = new ArrayList<AffiliateEntry>(); HashMap<String, Integer> columnIndex = new HashMap(); for (int col_index = 0; col_index < collection.get(0).length; col_index++) { columnIndex.put(collection.get(0)[col_index].trim(), col_index); } for (int i = 1; i < collection.size(); i++) { […]

破坏不工作在Java

我已经创build了一个简单的java程序来打开一个excel文件并写入数据,一旦数据写入,然后继续打开文件来查看电子表格: String[] cmdarray=new String[]{"cmd.exe","/c","C:\\Users\\Jason\\Documents\\*******\\********\\******.xls"}; Runtime runTime = Runtime.getRuntime(); Process process = runTime.exec(cmdarray); 作为学习曲线的一部分,然后尝试在10秒后closures文件: process.destroy(); 但是这不是closures窗口。 任何人都可以指出我要去哪里错了吗? 谢谢。 private static void OpenExcel() throws IOException { //Find the File and open it String[] cmdarray=new String[]{"cmd.exe","/c","C:\\Users\\Jason\\Documents\\*********\\*********\\********.xls"}; Runtime runTime = Runtime.getRuntime(); Process process = runTime.exec(cmdarray); try{ //Delay TimeUnit.SECONDS.sleep(10); }catch (InterruptedException e) { e.printStackTrace(); //Handle exception } //Close Excel System.out.println("Closing […]

如何为Grails编写XLSX自定义渲染器

我正在尝试使用grails自定义渲染器来渲染使用apache-poi库的Excel XLSX文件。 我做了一个渲染器类 class APIReportXLSXRenderer extends AbstractRenderer<APIReport> { APIReportXLSXRenderer() { super(APIReport, [new MimeType("application/vnd.ms-excel", "xlsx")] as MimeType[]) } @Override void render(APIReport output, RenderContext context) { context.contentType = GrailsWebUtil.getContentType("application/vnd.ms-excel", GrailsWebUtil.DEFAULT_ENCODING) def items = output.getItems() def fields = output.getFields() def headers = (fields.keySet() + items[0].keySet()) as List // convert maps to list of values each in order of […]