Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import jxl.Sheet;
  2. import jxl.Workbook;
  3. import org.testng.annotations.BeforeClass;
  4. import org.testng.annotations.Test;
  5. import jxl.write.*;
  6.  
  7. import java.io.*;
  8.  
  9. /**
  10. * Code to update 10 student records into table into Excel file
  11. */
  12. public class Practical_5 {
  13. @BeforeClass
  14. public void f1() {
  15. }
  16.  
  17. @Test
  18. public void testImportExport1() throws Exception {
  19. File xlsInputFile = new File("E:\\Ty\\ST\\input.xls"); // Get Inputs from this xls
  20. File xlsOutputFile = new File("E:\\Ty\\ST\\output.xls"); // Stream Output to this xls
  21.  
  22. FileInputStream fi = new FileInputStream(xlsInputFile);
  23. FileOutputStream fo = new FileOutputStream(xlsOutputFile);
  24.  
  25. Workbook inputWorkbook = Workbook.getWorkbook(fi);
  26. Sheet inputWorkbookSheet = inputWorkbook.getSheet(0);
  27. // Used to store content of inputWorkbook - later used to fetch marks obtained
  28. String workbookArray[][] = new String[inputWorkbookSheet.getRows()][inputWorkbookSheet.getColumns()];
  29.  
  30. WritableWorkbook outputWorkbook = Workbook.createWorkbook(fo);
  31. WritableSheet outputWorkbookSheet = outputWorkbook.createSheet("result1", 0);
  32.  
  33.  
  34. // Fetch and store input xls data, and populate output xls sheet
  35. for (int i = 0; i < inputWorkbookSheet.getRows(); i++) {
  36. for (int j = 0; j < inputWorkbookSheet.getColumns(); j++) {
  37. workbookArray[i][j] = inputWorkbookSheet.getCell(j, i).getContents();
  38. Label label = new Label(j, i, workbookArray[i][j]); // parameter of Label(Column, Row,cellFormat)
  39. outputWorkbookSheet.addCell(label);
  40. }
  41. }
  42. //Creates result column
  43. Label resultLabel = new Label(6, 0, "Result");
  44. outputWorkbookSheet.addCell(resultLabel);
  45.  
  46.  
  47. /* Compute's pass/fail.
  48.  
  49. Note: 1) row starts from 1 because 1st row consists of names
  50. 2) col starts from 2, to exclude rollno(idx=0),name(idx=1), and compute only marks.
  51. */
  52. for (int i = 1; i < inputWorkbookSheet.getRows(); i++) {
  53. for (int j = 2; j < inputWorkbookSheet.getColumns(); j++) {
  54. workbookArray[i][j] = inputWorkbookSheet.getCell(j, i).getContents();
  55. int x = Integer.parseInt(workbookArray[i][j]);
  56. if (x > 35) {
  57. Label l1 = new Label(6, i, "pass");
  58. outputWorkbookSheet.addCell(l1);
  59. } else {
  60. Label l1 = new Label(6, i, "fail");
  61. outputWorkbookSheet.addCell(l1);
  62. break;
  63. }
  64. }
  65. }
  66.  
  67. //close streams
  68. outputWorkbook.write();
  69. outputWorkbook.close();
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement