Guest User

Untitled

a guest
Sep 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. function ParseDocument() {
  2.  
  3. var table = document.getElementById('allContact');
  4. var contactDTOList = new Array();
  5. var i =0;
  6.  
  7. for (var r = 2, n = table.rows.length; r < n; r++) {
  8.  
  9. if (table.rows[r].style.display !== 'none') {
  10. var s = table.rows[r].cells[0].innerHTML;
  11. var bits = s.split(/D/);
  12. var date = new Date(bits[0], --bits[1], bits[2], bits[3], bits[4], bits[5]);
  13.  
  14. var contactDTO = {
  15. dateOfBirth:date.getTime(),
  16. firstName:table.rows[r].cells[1].innerHTML,
  17. lastName:table.rows[r].cells[2].innerHTML,
  18. email:table.rows[r].cells[3].innerHTML
  19. };
  20. contactDTOList.push(contactDTO);
  21. i++;
  22. }
  23. }
  24.  
  25. $.ajax({
  26. type: 'POST',
  27. url: "/rest/bot5",
  28. data: JSON.stringify(contactDTOList),
  29. contentType: "application/json;charset=UTF-8",
  30. async: true,
  31. success: function () {
  32. }
  33. });
  34. }
  35.  
  36. import org.springframework.http.MediaType;
  37. import org.springframework.web.bind.annotation.*;
  38. import org.apache.poi.ss.usermodel.Cell;
  39. import org.apache.poi.ss.usermodel.CellStyle;
  40. import org.apache.poi.ss.usermodel.Font;
  41. import org.apache.poi.ss.usermodel.IndexedColors;
  42. import org.apache.poi.ss.usermodel.Row;
  43. import org.apache.poi.ss.usermodel.Sheet;
  44. import org.apache.poi.ss.usermodel.Workbook;
  45. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  46.  
  47. import java.io.FileOutputStream;
  48. import java.io.IOException;
  49. import java.util.ArrayList;
  50. import java.util.List;
  51.  
  52. @RestController
  53. @RequestMapping(value = "/test")
  54. public class BotRestController {
  55.  
  56. @PostMapping(value = "/bot5", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  57. public FileOutputStream postOrderingTask(@RequestBody List<Contact> contactList) throws IOException {
  58.  
  59. String[] columns = {"First Name", "Last Name", "Email", "Date Of Birth"};
  60. List<Contact> contacts = new ArrayList<Contact>();
  61.  
  62. contacts.add(new Contact("Sylvain", "Saurel", "sylvain.saurel@gmail.com", "2018-09-25 16:28:47"));
  63. contacts.add(new Contact("Albert", "Dupond", "sylvain.saurel@gmail.com", "2018-09-25 17:28:47"));
  64. contacts.add(new Contact("Pierre", "Dupont", "sylvain.saurel@gmail.com", "2018-09-25 18:28:47"));
  65. contacts.add(new Contact("Mariano", "Diaz", "sylvain.saurel@gmail.com", "2018-09-25 19:28:47"));
  66.  
  67. Workbook workbook = new XSSFWorkbook();
  68. Sheet sheet = workbook.createSheet("Contacts");
  69.  
  70. Font headerFont = workbook.createFont();
  71. headerFont.setBold(true);
  72. headerFont.setFontHeightInPoints((short) 14);
  73. headerFont.setColor(IndexedColors.RED.getIndex());
  74.  
  75. CellStyle headerCellStyle = workbook.createCellStyle();
  76. headerCellStyle.setFont(headerFont);
  77.  
  78. // Create a Row
  79. Row headerRow = sheet.createRow(0);
  80.  
  81. for (int i = 0; i < columns.length; i++) {
  82. Cell cell = headerRow.createCell(i);
  83. cell.setCellValue(columns[i]);
  84. cell.setCellStyle(headerCellStyle);
  85. }
  86.  
  87. // Create Other rows and cells with contacts data
  88. int rowNum = 1;
  89. for (Contact contact : contacts) {
  90. Row row = sheet.createRow(rowNum++);
  91. row.createCell(0).setCellValue(contact.firstName);
  92. row.createCell(1).setCellValue(contact.lastName);
  93. row.createCell(2).setCellValue(contact.email);
  94. row.createCell(3).setCellValue(contact.dateOfBirth);
  95. }
  96.  
  97. // Resize all columns to fit the content size
  98. for (int i = 0; i < columns.length; i++) {
  99. sheet.autoSizeColumn(i);
  100. }
  101.  
  102. // Write the output to a file
  103. FileOutputStream fileOut = new FileOutputStream("contacts.xlsx");
  104. workbook.write(fileOut);
  105. fileOut.close();
  106. return fileOut;
  107. }
  108. }
Add Comment
Please, Sign In to add comment