java - Write items into excel cell -
I'm absolutely new to Eclips Java coding. I'm trying to finish a project to manage inventory. Part I is having trouble with that, when I tried to write an item in Excel cell, I was saying that the array is getting out of bounds errors.
PS:. Item and item.getPartname etc. are all defined under another category file. Please help. Thanks
FileOptStrap OS = new file output stream ("Order Receipt"); // Create a new workbook for writing data HSSFWorkbook wb2 = New HSSFWorkbook (); // Create a new sheet: HSS FSshit newsletter = wb2.createSheet ("MyNewSheet"); // Create a new line: for (int i = 0; i
I can see many problems with the attached code:
-
Missing file extension when creating new FileOutputStream - Since you are generating .xls workbook, you probably want to store in the XLS file (the extension is automatically ), Also make sure that you have the correct path to the directory for permission to write (Local App Direction, like this Condition should be fine). -
As you've already mentioned, you're resetting the same cell value 4 times
-
You are creating the same cell style many times (there is no cache behind this scenario and the number of cells at large is limited which can be made so that you are creating thousands of rows which can get you into problems < / P>
-
You can write your workbook to flush () and off () Streams in Java. Streams in Java are a valuable resource that should be manually closed. -
Unless the stack trace comes from say 100% where the ArrayOutOfBound issue you are viewing Still, my guess would be that you are doing it is trying to reach items with that index (from the collection of objects), which is not in existence, as a result of which you have a list of your report data data Instead, you are running from row indexes.
In addition, since you have some guidelines in New to Java, which will give you better and less error prone codes in future Will allow:
-
Try to split your code into smaller, more testable modules i.e. you can be a helper createDataRow method in your main method In general, if one method is more than some inner loops, it makes them incredibly difficult to test, debug, and cause. -
Unless you really need to generate .xls format, consider using XSSF * Classes to create xlsx documents -. (Much better dataFormat support) It has many improvements on HSSF *
In my mind, I am getting my example rewritten:
<0> Public zero correction ( item) of the list throws IOException {HSSFWorkbook workbook = new HSSFWorkbook (); HSSFSheet sheet = workbook.createSheet ("MyNewSheet"); HSSFCell style styleFeel = workbook .createCellStyle (); StyleOfCell.setDataFormat (HSSFDataFormat.getBuiltinFormat ("d / m / yy")); StyleOfCell.setFillForegroundColor (HSSFColor.AQUA.index); StyleOfCell.setFillPattern (HSSFCellStyle.BORDER_THIN); Int rowIndex = 0; (SentOrder Item: Item) {HSSFRow line = sheet.createRow (rowIndex ++); HSSFCL name cell = row.Createel (0); NameCell.setCellValue (item.getPartName ()); HSSFCL NumberKell = Row.Createel (1); NumberCell.setCellValue (item.getPartNumber ()); HSSFCell Volume Call = Line Create cell (2); QuantityCell.setCellValue (item.getQuantity ()); HSSFCell dateCell = Row.Createel (3); DateCell.setCellValue (new date ()); DateCell.setCellStyle (styleOfCell); } FileOutputStream OS = New FileOutputStream ("order_receipt.xls"); Try {workbook.write (OS); } Finally {os.flush (); Os.close (); }}
Comments
Post a Comment