PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ds/pa3/ExcelFileOut.java

https://bitbucket.org/jb007/pa4_17.6.
Java | 74 lines | 58 code | 14 blank | 2 comment | 10 complexity | b23ec4e118ba35f66f7e3561303b5798 MD5 | raw file
  1. package ds.pa3;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFRow;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. public class ExcelFileOut {
  11. /** This method writes data to new excel file **/
  12. public static void writeDataToExcelFile(String fileName, long[][] data, int numDeleteRows) {
  13. String [] headers = {"P1-#sent_msg", "P1-#received_msg", "P1-#sent_bytes",
  14. "P2-#sent_msg", "P2-#received_msg", "P2-#sent_bytes",
  15. "P3-#sent_msg", "P3-#received_msg", "P3-#sent_bytes",
  16. "P4-#sent_msg", "P4-#received_msg", "P4-#sent_bytes",
  17. "P5-#sent_msg", "P5-#received_msg", "P5-#sent_bytes",
  18. "P6-#sent_msg", "P6-#received_msg", "P6-#sent_bytes",
  19. "P7-#sent_msg", "P7-#received_msg", "P7-#sent_bytes",
  20. "P8-#sent_msg", "P8-#received_msg", "P8-#sent_bytes",};
  21. long[][] excelData = data;
  22. File f = new File(fileName);
  23. InputStream is;
  24. HSSFWorkbook myWorkBook;
  25. HSSFSheet mySheet;
  26. HSSFRow myRow = null;
  27. HSSFCell myCell = null;
  28. try{
  29. if(f.exists()) {
  30. is = new FileInputStream(fileName);
  31. myWorkBook = new HSSFWorkbook(is);
  32. mySheet = myWorkBook.getSheetAt(0);
  33. //clear
  34. for (int i=0; i<numDeleteRows; i++) {
  35. if (mySheet.getRow(i) != null) {
  36. mySheet.removeRow(mySheet.getRow(i));
  37. }
  38. }
  39. myWorkBook.getSheetAt(1).getRow(1).createCell(1).setCellValue(TestHarness.TEST_PERIOD);
  40. } else {
  41. myWorkBook = new HSSFWorkbook();
  42. mySheet = myWorkBook.createSheet();
  43. }
  44. for (int rowNum = 0; rowNum <= excelData.length; rowNum++){
  45. myRow = mySheet.createRow(rowNum);
  46. for (int cellNum = 0; cellNum < (TestHarness.MAX_NUM_OF_MACHINES * TestHarness.NUM_MSGSTAT_INFOS) ; cellNum++){
  47. myCell = myRow.createCell(cellNum);
  48. if (rowNum == 0) {
  49. myCell.setCellValue(headers[cellNum]);
  50. } else {
  51. myCell.setCellValue(excelData[rowNum-1][cellNum]);
  52. }
  53. }
  54. }
  55. FileOutputStream out = new FileOutputStream(fileName);
  56. myWorkBook.write(out);
  57. out.close();
  58. }catch(Exception e){ e.printStackTrace();}
  59. }
  60. }