From f44732045318f707126b334be781d175f57c8acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E7=A5=A5?= Date: Sat, 5 Jul 2025 18:12:04 +0800 Subject: [PATCH] commit a project on new repository --- .lightly/settings.toml | 6 + JavaProject/.lightly/settings.toml | 6 + JavaProject/pom.xml | 69 +++++++++++ .../java/com/example/demo/Application.java | 13 ++ .../demo/controller/ExcelConvertHtml.java | 98 +++++++++++++++ .../demo/controller/HelloController.java | 14 +++ .../src/main/resources/application.properties | 1 + .../com/example/demo/ApplicationTests.java | 13 ++ output.html | 90 ++++++++++++++ pom.xml | 78 ++++++++++++ .../java/com/example/demo/Application.java | 13 ++ .../example/demo/CalculatorPrefectNumber.java | 42 +++++++ .../com/example/demo/LinkedListRemover.java | 96 +++++++++++++++ .../example/demo/PrefectNumberCalculator.java | 44 +++++++ .../example/demo/RemoveReversePosition.java | 92 ++++++++++++++ src/main/java/com/example/demo/Test.java | 28 +++++ .../example/demo/TestLinkedListRemover.java | 83 +++++++++++++ .../demo/controller/ExcelConvertHtml.java | 113 ++++++++++++++++++ .../demo/controller/HelloController.java | 16 +++ .../demo/service/ExcelToHtmlConverter.java | 95 +++++++++++++++ .../java/com/example/demo/service/input.xlsx | Bin 0 -> 17736 bytes src/main/resources/application.properties | 1 + .../com/example/demo/ApplicationTests.java | 13 ++ 23 files changed, 1024 insertions(+) create mode 100644 .lightly/settings.toml create mode 100644 JavaProject/.lightly/settings.toml create mode 100644 JavaProject/pom.xml create mode 100644 JavaProject/src/main/java/com/example/demo/Application.java create mode 100644 JavaProject/src/main/java/com/example/demo/controller/ExcelConvertHtml.java create mode 100644 JavaProject/src/main/java/com/example/demo/controller/HelloController.java create mode 100644 JavaProject/src/main/resources/application.properties create mode 100644 JavaProject/src/test/java/com/example/demo/ApplicationTests.java create mode 100644 output.html create mode 100644 pom.xml create mode 100644 src/main/java/com/example/demo/Application.java create mode 100644 src/main/java/com/example/demo/CalculatorPrefectNumber.java create mode 100644 src/main/java/com/example/demo/LinkedListRemover.java create mode 100644 src/main/java/com/example/demo/PrefectNumberCalculator.java create mode 100644 src/main/java/com/example/demo/RemoveReversePosition.java create mode 100644 src/main/java/com/example/demo/Test.java create mode 100644 src/main/java/com/example/demo/TestLinkedListRemover.java create mode 100644 src/main/java/com/example/demo/controller/ExcelConvertHtml.java create mode 100644 src/main/java/com/example/demo/controller/HelloController.java create mode 100644 src/main/java/com/example/demo/service/ExcelToHtmlConverter.java create mode 100644 src/main/java/com/example/demo/service/input.xlsx create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/com/example/demo/ApplicationTests.java diff --git a/.lightly/settings.toml b/.lightly/settings.toml new file mode 100644 index 0000000..0445325 --- /dev/null +++ b/.lightly/settings.toml @@ -0,0 +1,6 @@ +ports = [] +[languages.java] +language = "java" +src_folder = "src/main/java/" + +[languages.java.run] diff --git a/JavaProject/.lightly/settings.toml b/JavaProject/.lightly/settings.toml new file mode 100644 index 0000000..0445325 --- /dev/null +++ b/JavaProject/.lightly/settings.toml @@ -0,0 +1,6 @@ +ports = [] +[languages.java] +language = "java" +src_folder = "src/main/java/" + +[languages.java.run] diff --git a/JavaProject/pom.xml b/JavaProject/pom.xml new file mode 100644 index 0000000..7e3c7d4 --- /dev/null +++ b/JavaProject/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.1.RELEASE + + + com.example + JavaProject + 1.0.0-SNAPSHOT + SpringBootProject + Demo project for Spring Boot + + 11 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.apache.poi + poi + 3.17 + + + org.apache.poi + poi-ooxml + 3.17 + + + org.apache.poi + poi-scratchpad + 3.17 + + + + io.springfox + springfox-swagger2 + 2.8.0 + + + io.springfox + springfox-swagger-ui + 2.8.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/JavaProject/src/main/java/com/example/demo/Application.java b/JavaProject/src/main/java/com/example/demo/Application.java new file mode 100644 index 0000000..9b21b25 --- /dev/null +++ b/JavaProject/src/main/java/com/example/demo/Application.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/JavaProject/src/main/java/com/example/demo/controller/ExcelConvertHtml.java b/JavaProject/src/main/java/com/example/demo/controller/ExcelConvertHtml.java new file mode 100644 index 0000000..df299c5 --- /dev/null +++ b/JavaProject/src/main/java/com/example/demo/controller/ExcelConvertHtml.java @@ -0,0 +1,98 @@ +package com.example.demo.controller; + +public class ExcelConvertHtml { + + public String static resolveExcel(MultipartFile file) throws Exception { + try { + File toFile = null; + if (file.equals("") || file.getSIze() <= 0) { + file = null; + } else { + InputStream ins = null; + ins = file.getInputStream(): + toFile = new File(file.getOriginalFilename()); + inputStreamToFile(ins, toFile); + ins.close(); + } + // 获取数据 + FileInputStream input = new FileInputStream(toFile); + Workbook workbook = new XSSFWorkbook(input); + Sheet sheet = workbook.getSheetAt(0); + // create html table + Element table = new Element(Tag.valueOf("table"), ""); + table.attr("border", "1"); + table.attr("cellspacing", "0"); + table.attr("cellpadding", "0"); + // Add column widths + Element colgroup = new Element(Tag.valueOf("colgroup"), ""); + for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) { + Element col = new Element(Tag.valueOf("col"), ""); + col.attr("width", "150px");// set column width + colgroup.appendChild(col); + } + table.appendChild(colgroup); + + // Add header row + Element thead = new Element(Tag.valueOf("thread"), ""); + Element tr = new Element(Tag.valueOf("tr"), ""); + tr.attr("bgcolor", "#CCCCCC"); + for (int i = 0; i < 2; i++) { + Cell cell = sheet.getRow(0).getCell(i); + String value = cell.getStringCellValue(); + Element th = new Element(Tag.valueOf("th"), ""); + th.text(value); + if (i == 0) { + // Merge first two columns + th.attr("colspan", "2"); + } + tr.appendChild(th); + } + for (int i = 2; i < sheet,getRow(0).getLastCellNum(); + i++){ + Cell cell = sheet.getRow(0).getCell(i); + String value = cell.getStringCellValue(); + Element th = new Element(Tag.valueOf("th"), ""); + th.text(value); + tr.appendChild(th); + } + thread.appendChild(tr); + table.appendChild(thread); + + // Add data rows + Element tbody = new Element(Tag.valueOf("tbody"), ""); + // for (int i = 1; i <= sheet.getLastRowNum(); i++) { + // Element trData + // } + for (int i = 1; i <= sheet.getLastRowNum(); i++) { + Element trData = new Element(Tag.valueOf("tr"), ""); + for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) { + Cell cell = sheet.getRow(i).getCell(j); + String value = ""; + if (cell.getCellType() == CellType.NUMERIC) { + value = String.valueOf(cell.getNumericCellValue()); + } else { + value = cell.getStringCellValue(); + } + Element td = new Element(Tag.valueOf("td"), ""); + td.text(value); + if (j < 2) { + td.attr("align", "left"); + } + trData.appendChild(td); + } + tbody.appendChild(trData); + } + table.appendChild(tbody); // Convert to HTML + Document doc = new Document(""); + doc.appendChild(table); + String html = doc.outerHtml(); + // Write to file + FileWriter writer = new FileWriter(new File("output.html")); + writer.write(html); + writer.close(); + } catch (Exception ex) { + System.out.println(ex.getMessage()); + } + } +} +} \ No newline at end of file diff --git a/JavaProject/src/main/java/com/example/demo/controller/HelloController.java b/JavaProject/src/main/java/com/example/demo/controller/HelloController.java new file mode 100644 index 0000000..5ba8c5f --- /dev/null +++ b/JavaProject/src/main/java/com/example/demo/controller/HelloController.java @@ -0,0 +1,14 @@ +package com.example.demo.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@Controller +@RestMapping +public class HelloController { + + @GetMapping(value = "/") + public String index() { + return "Hello World!"; + } +} diff --git a/JavaProject/src/main/resources/application.properties b/JavaProject/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/JavaProject/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/JavaProject/src/test/java/com/example/demo/ApplicationTests.java b/JavaProject/src/test/java/com/example/demo/ApplicationTests.java new file mode 100644 index 0000000..70a27d2 --- /dev/null +++ b/JavaProject/src/test/java/com/example/demo/ApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/output.html b/output.html new file mode 100644 index 0000000..9a615d0 --- /dev/null +++ b/output.html @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
主机名管理IP数据IP所在资源池操作系统设备编码所在楼宇机房机架机位设备序列号配置信息CPU型号内存型号内存数vcore数磁盘容量磁盘详细信息主机描述
hostname1127.0.0.110.10.10.1第一资源池CentOSA01-01-101-1A01A0111011A011234567891*1C/1GB/1*1GSSD/1*1TBSATA型号1型号11GB11TB1*1TB模板主机
hostname2127.0.0.210.10.10.2第二资源池CentOSA01-01-101-1A02A0211011A022234567891*1C/1GB/1*1GSSD/1*1TBSATA型号2型号21GB11TB1*1TB模板主机
\ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6b3a96f --- /dev/null +++ b/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.1.RELEASE + + + com.example + JavaProject + 1.0.0-SNAPSHOT + SpringBootProject + Demo project for Spring Boot + + 8 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.apache.poi + poi + 3.17 + + + org.apache.poi + poi-ooxml + 3.17 + + + org.apache.poi + poi-scratchpad + 3.17 + + + + io.springfox + springfox-swagger2 + 2.8.0 + + + io.springfox + springfox-swagger-ui + 2.8.0 + + + org.junit.jupiter + junit-jupiter-api + + + org.jsoup + jsoup + 1.10.2 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/example/demo/Application.java b/src/main/java/com/example/demo/Application.java new file mode 100644 index 0000000..9b21b25 --- /dev/null +++ b/src/main/java/com/example/demo/Application.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/src/main/java/com/example/demo/CalculatorPrefectNumber.java b/src/main/java/com/example/demo/CalculatorPrefectNumber.java new file mode 100644 index 0000000..5d44651 --- /dev/null +++ b/src/main/java/com/example/demo/CalculatorPrefectNumber.java @@ -0,0 +1,42 @@ +package com.example.demo; + +/** + * @author shixiang + * @date 2025/7/1 14:01 + */ +public class CalculatorPrefectNumber { + public static int countPrefectNumber(int n) { + if (n < 6) { + return 0; + } + int count = 0; + for (int num = 6; num <= n; num++) { + if (isPrefectNumber(num)) { + count++; + } + } + return count; + } + + private static boolean isPrefectNumber(int num) { + if (num < 2) { + return false; + } + int sum = 1; + for (int i = 2; i * i <= num; i++) { + if (num % i == 0) { + sum += i; + if (i != num / i) { + sum += num / i; + } + } + } + return num == sum; + } + + public static void main(String[] args) { + int i = countPrefectNumber(6); + int i1 = countPrefectNumber(28); + System.out.println("i=" + i + ",i1=" + i1); + } +} diff --git a/src/main/java/com/example/demo/LinkedListRemover.java b/src/main/java/com/example/demo/LinkedListRemover.java new file mode 100644 index 0000000..9e0dfc2 --- /dev/null +++ b/src/main/java/com/example/demo/LinkedListRemover.java @@ -0,0 +1,96 @@ +package com.example.demo; + +/** + * @author shixiang + * @date 2025/7/1 13:36 + */ + +// 单向链表节点类(使用无参构造) +class ListNode { + int val; + ListNode next; + + // 无参构造函数 + public ListNode() { + } + + // 带值的构造函数(可选) + public ListNode(int val) { + this.val = val; + } +} + +public class LinkedListRemover { + + public ListNode removeNthFromEnd(ListNode head, int n) { + // 创建虚拟头节点(使用无参构造) + ListNode dummy = new ListNode(); + dummy.next = head; // 指向实际头节点 + + // 初始化快慢指针 + ListNode fast = dummy; + ListNode slow = dummy; + + // 快指针先前进n步 + for (int i = 0; i < n; i++) { + fast = fast.next; + } + + // 同时移动双指针,直到快指针到达最后一个节点 + while (fast.next != null) { + fast = fast.next; + slow = slow.next; + } + + // 删除目标节点:跳过要移除的节点 + slow.next = slow.next.next; + + // 返回新的头节点(虚拟头节点的下一个) + return dummy.next; + } + + // 辅助方法:打印链表(用于测试) + public static void printList(ListNode head) { + ListNode current = head; + while (current != null) { + System.out.print(current.val + " "); + current = current.next; + } + System.out.println(); + } + + // 测试用例 + public static void main(String[] args) { + // 创建链表 1->2->3->4->5 + ListNode head = new ListNode(); + head.val = 1; + head.next = new ListNode(); + head.next.val = 2; + head.next.next = new ListNode(); + head.next.next.val = 3; + head.next.next.next = new ListNode(); + head.next.next.next.val = 4; + head.next.next.next.next = new ListNode(); + head.next.next.next.next.val = 5; + + LinkedListRemover remover = new LinkedListRemover(); + + System.out.print("原始链表: "); + printList(head); // 输出: 1 2 3 4 5 + + // 移除倒数第2个节点(值为4) + head = remover.removeNthFromEnd(head, 4); + System.out.print("移除后: "); + printList(head); // 输出: 1 2 3 5 + + // 测试边界情况:移除头节点(倒数第4个) + head = remover.removeNthFromEnd(head, 4); + System.out.print("移除头节点后: "); + printList(head); // 输出: 2 3 5 + + // 测试边界情况:移除尾节点(倒数第3个) + head = remover.removeNthFromEnd(head, 3); + System.out.print("移除尾节点后: "); + printList(head); // 输出: 3 5 + } +} diff --git a/src/main/java/com/example/demo/PrefectNumberCalculator.java b/src/main/java/com/example/demo/PrefectNumberCalculator.java new file mode 100644 index 0000000..4a16e70 --- /dev/null +++ b/src/main/java/com/example/demo/PrefectNumberCalculator.java @@ -0,0 +1,44 @@ +package com.example.demo; + +/** + * @author shixiang + * @date 2025/7/1 12:37 + */ +public class PrefectNumberCalculator { + public static int countPerfectNumber(int n) { + if (n < 6) { + return 0; + } + int count = 0; + for (int num = 6; num <= n; num++) { + if (isPerfectNumber(num)) { + count++; + } + } + return count; + } + + private static boolean isPerfectNumber(int num) { + if (num < 2) { + return false; + } + int sum = 1; + for (int i = 2; i * i <= num; i++) { + if (num % i == 0) { + sum += i; + if (i != num / i) { + sum += num / i; + } + } + } + return sum == num; + + } + + public static void main(String[] args) { + int i1 = countPerfectNumber(6); + int i = countPerfectNumber(28); + int i2 = countPerfectNumber(29); + System.out.println("i1" + i1 + ",i2=" + i2 + ",i=" + i); + } +} diff --git a/src/main/java/com/example/demo/RemoveReversePosition.java b/src/main/java/com/example/demo/RemoveReversePosition.java new file mode 100644 index 0000000..3f12130 --- /dev/null +++ b/src/main/java/com/example/demo/RemoveReversePosition.java @@ -0,0 +1,92 @@ +package com.example.demo; + +/** + * 移除指定倒数位置的单向链表位置 + * + * @author shixiang + * @date 2025/7/3 10:26 + */ + +public class RemoveReversePosition { + + static class ListNode { + int val; + ListNode next; + + // 无参构造函数 + public ListNode() { + } + + // 带值的构造函数(可选) + public ListNode(int val) { + this.val = val; + } + } + + /** + * 具体思路:我们通过快慢指针(双指针方式)做处理即可 + * 比如1-2-3-4-5 n=2 ==>本质上就是取第4位的节点并删除=》其实就是将1-2-3-5 即找到第三个节点,然后使用current.next= current.next.next; + * 其实本质上就是找到倒数n-1的位置,所以假设链表长度为L 那么这个位置就是L-n(比如L等于5,那么n=2,即需要找到第三个的位置即L-n + * + * @param head + * @param n + * @return + */ + public static ListNode removeList(ListNode head, int n) { + // 定义一个虚拟节点用于关联最初始的链表 + ListNode dummp = new ListNode(); + dummp.next = head; + ListNode fast = dummp; + ListNode slow = dummp; + //那么我们先移动fast的位置n个位置 此时fast的后续位置为L-N; + for (int i = 0; i < n; i++) { + fast = fast.next; + } + // 所以这个时候我们只需要判断fast.next!=null,然后同时移动fast和slow即可 + while (fast.next != null) { + fast = fast.next; + slow = slow.next; + } + // 此时slow的节点位置在L-N上 + // 那么只需要去删除slow的下一节点位置即可 + slow.next = slow.next.next; + // 注意这里slow节点与最初始的时候dummp定义的引用地址保持一致。 + return dummp.next; + } + + public static void main(String[] args) { + // 1,2,3,4,5 n =2 =>1,2,3,5 + ListNode test1 = new ListNode(1); + test1.next = new ListNode(2); + test1.next.next = new ListNode(3); + test1.next.next.next = new ListNode(4); + test1.next.next.next.next = new ListNode(5); + printList(test1); + ListNode listNode1 = removeList(test1, 2); + printList(listNode1); + ListNode test2 = new ListNode(1); + test2.next = new ListNode(2); + printList(test2); + ListNode listNode2 = removeList(test2, 1); + printList(listNode2); + ListNode test3 = new ListNode(1); + printList(test3); + ListNode listNode3 = removeList(test3, 1); + printList(listNode3); + } + + // 辅助方法:打印链表(用于测试) + public static void printList(ListNode head) { + ListNode current = head; + if (current == null) { + System.out.print("[]"); + return; + } + while (current != null) { + System.out.print(current.val + " "); + current = current.next; + } + System.out.println(); + } + +} diff --git a/src/main/java/com/example/demo/Test.java b/src/main/java/com/example/demo/Test.java new file mode 100644 index 0000000..55795d0 --- /dev/null +++ b/src/main/java/com/example/demo/Test.java @@ -0,0 +1,28 @@ +package com.example.demo; + +/** + * @author shixiang + * @date 2025/7/1 16:04 + */ +public class Test { + // n= 1 1 + // n = 2 2 + // n = 3 3 + // n = 4 5 + public static int getMethod(int n) { + if (n <= 1) { + return 1; + } + if (n == 2) { + return 2; + } + int method = getMethod(n - 1); + int method1 = getMethod(n-2); + return method+method1; + } + + public static void main(String[] args) { + int method = getMethod(5); + + } +} diff --git a/src/main/java/com/example/demo/TestLinkedListRemover.java b/src/main/java/com/example/demo/TestLinkedListRemover.java new file mode 100644 index 0000000..12011e1 --- /dev/null +++ b/src/main/java/com/example/demo/TestLinkedListRemover.java @@ -0,0 +1,83 @@ +package com.example.demo; + +/** + * @author shixiang + * @date 2025/7/1 13:51 + */ +public class TestLinkedListRemover { + // 单向链表节点类(使用无参构造) + static class ListNode { + int val; + ListNode next; + + // 无参构造函数 + public ListNode() { + } + + // 带值的构造函数(可选) + public ListNode(int val) { + this.val = val; + } + } + + public static void main(String[] args) { + // 创建链表 1->2->3->4->5 + ListNode head = new ListNode(); + head.val = 1; + head.next = new ListNode(); + head.next.val = 2; + head.next.next = new ListNode(); + head.next.next.val = 3; + head.next.next.next = new ListNode(); + head.next.next.next.val = 4; + head.next.next.next.next = new ListNode(); + head.next.next.next.next.val = 5; + + System.out.print("原始链表: "); + printList(head); // 输出: 1 2 3 4 5 + ListNode listNode = removeList(head, 2); + System.out.print("移除指定位置: "); + printList(listNode); // 输出: 1 2 3 4 5 + + ListNode head1 = new ListNode(); + head1.val = 1; + head1.next = new ListNode(); + head1.next.val = 2; + ListNode listNode1 = removeList(head1, 1); + System.out.print("移除指定位置: "); + printList(listNode1); + + } + + // 辅助方法:打印链表(用于测试) + public static void printList(ListNode head) { + ListNode current = head; + while (current != null) { + System.out.print(current.val + " "); + current = current.next; + } + System.out.println(); + } + + public static ListNode removeList(ListNode node, int n) { + ListNode dumpNode = new ListNode(); + dumpNode.next = node; + ListNode fast = dumpNode; + ListNode slow = dumpNode; + + // 先移动n步 + for (int i = 0; i < n; i++) { + fast = fast.next; + } + // 这里移动慢指针L-n步骤 + while (fast.next != null) { + fast = fast.next; + slow = slow.next; + } + slow.next = slow.next.next; + return dumpNode.next; + } + +} + + diff --git a/src/main/java/com/example/demo/controller/ExcelConvertHtml.java b/src/main/java/com/example/demo/controller/ExcelConvertHtml.java new file mode 100644 index 0000000..c3014d5 --- /dev/null +++ b/src/main/java/com/example/demo/controller/ExcelConvertHtml.java @@ -0,0 +1,113 @@ +//package com.example.demo.controller; +// +//import org.apache.poi.ss.usermodel.Cell; +//import org.apache.poi.ss.usermodel.CellType; +//import org.apache.poi.ss.usermodel.Sheet; +//import org.apache.poi.ss.usermodel.Workbook; +//import org.apache.poi.xssf.usermodel.XSSFWorkbook; +//import org.jsoup.*; +//import org.jsoup.nodes.*; +//import org.jsoup.parser.*; +//import org.jsoup.select.*; +//import org.springframework.web.multipart.MultipartFile; +// +//import java.io.File; +//import java.io.FileInputStream; +//import java.io.FileWriter; +//import java.io.InputStream; +// +//public class ExcelConvertHtml { +// +// public String static resolveExcel(MultipartFile file) throws Exception { +// try { +// File toFile = null; +// if (file.equals("") || file.getSize() <= 0) { +// file = null; +// } else { +// InputStream ins = null; +// ins = file.getInputStream(); +// toFile = new File(file.getOriginalFilename()); +// inputStreamToFile(ins, toFile); +// ins.close(); +// } +// // 获取数据 +// FileInputStream input = new FileInputStream(toFile); +// Workbook workbook = new XSSFWorkbook(input); +// Sheet sheet = workbook.getSheetAt(0); +// // create html table +// Element table = new Element(Tag.valueOf("table"), ""); +// table.attr("border", "1"); +// table.attr("cellspacing", "0"); +// table.attr("cellpadding", "0"); +// // Add column widths +// Element colgroup = new Element(Tag.valueOf("colgroup"), ""); +// for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) { +// Element col = new Element(Tag.valueOf("col"), ""); +// col.attr("width", "150px");// set column width +// colgroup.appendChild(col); +// } +// table.appendChild(colgroup); +// +// // Add header row +// Element thead = new Element(Tag.valueOf("thread"), ""); +// Element tr = new Element(Tag.valueOf("tr"), ""); +// tr.attr("bgcolor", "#CCCCCC"); +// for (int i = 0; i < 2; i++) { +// Cell cell = sheet.getRow(0).getCell(i); +// String value = cell.getStringCellValue(); +// Element th = new Element(Tag.valueOf("th"), ""); +// th.text(value); +// if (i == 0) { +// // Merge first two columns +// th.attr("colspan", "2"); +// } +// tr.appendChild(th); +// } +// for (int i = 2; i < sheet.getRow(0).getLastCellNum(); i++) { +// Cell cell = sheet.getRow(0).getCell(i); +// String value = cell.getStringCellValue(); +// Element th = new Element(Tag.valueOf("th"), ""); +// th.text(value); +// tr.appendChild(th); +// } +// thread.appendChild(tr); +// table.appendChild(thread); +// +// // Add data rows +// Element tbody = new Element(Tag.valueOf("tbody"), ""); +// // for (int i = 1; i <= sheet.getLastRowNum(); i++) { +// // Element trData +// // } +// for (int i = 1; i <= sheet.getLastRowNum(); i++) { +// Element trData = new Element(Tag.valueOf("tr"), ""); +// for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) { +// Cell cell = sheet.getRow(i).getCell(j); +// String value = ""; +// if (cell.getCellType() == CellType.NUMERIC) { +// value = String.valueOf(cell.getNumericCellValue()); +// } else { +// value = cell.getStringCellValue(); +// } +// Element td = new Element(Tag.valueOf("td"), ""); +// td.text(value); +// if (j < 2) { +// td.attr("align", "left"); +// } +// trData.appendChild(td); +// } +// tbody.appendChild(trData); +// } +// table.appendChild(tbody); // Convert to HTML +// Document doc = new Document(""); +// doc.appendChild(table); +// String html = doc.outerHtml(); +// // Write to file +// FileWriter writer = new FileWriter(new File("output.html")); +// writer.write(html); +// writer.close(); +// } catch (Exception ex) { +// System.out.println(ex.getMessage()); +// } +// } +//} +//} \ No newline at end of file diff --git a/src/main/java/com/example/demo/controller/HelloController.java b/src/main/java/com/example/demo/controller/HelloController.java new file mode 100644 index 0000000..302211b --- /dev/null +++ b/src/main/java/com/example/demo/controller/HelloController.java @@ -0,0 +1,16 @@ +package com.example.demo.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Controller +@RequestMapping +public class HelloController { + + @GetMapping(value = "/") + public String index() { + return "Hello World!"; + } +} diff --git a/src/main/java/com/example/demo/service/ExcelToHtmlConverter.java b/src/main/java/com/example/demo/service/ExcelToHtmlConverter.java new file mode 100644 index 0000000..925aa3c --- /dev/null +++ b/src/main/java/com/example/demo/service/ExcelToHtmlConverter.java @@ -0,0 +1,95 @@ +package com.example.demo.service; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; + + +public class ExcelToHtmlConverter { + public static void main(String[] args) { + try { + // C:\\Users\\Lenovo\\Desktop\\1 + FileInputStream input = new FileInputStream(new File("D:\\uploads\\input.xlsx")); + Workbook workbook = new XSSFWorkbook(input); + Sheet sheet = workbook.getSheetAt(0); + // Create HTML table + Element table = new Element(Tag.valueOf("table"), ""); + table.attr("border", "1"); + table.attr("cellspacing", "0"); + table.attr("cellpadding", "10"); + // Add column widths + Element colgroup = new Element(Tag.valueOf("colgroup"), ""); + for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) { + Element col = new Element(Tag.valueOf("col"), ""); + // Set column width + col.attr("width", "100px"); + colgroup.appendChild(col); + } + table.appendChild(colgroup); + // Add header row + Element thead = new Element(Tag.valueOf("thead"), ""); + Element tr = new Element(Tag.valueOf("tr"), ""); + tr.attr("bgcolor", "#CCCCCC"); // Set background color + for (int i = 0; i < 2; i++) { + Cell cell = sheet.getRow(0).getCell(i); + String value = cell.getStringCellValue(); + Element th = new Element(Tag.valueOf("th"), ""); + th.text(value); + if (i == 0) { + th.attr("colspan", "2"); // Merge first two columns + } + tr.appendChild(th); + } + for (int i = 2; i < sheet.getRow(0).getLastCellNum(); i++) { + Cell cell = sheet.getRow(0).getCell(i); + String value = cell.getStringCellValue(); + Element th = new Element(Tag.valueOf("th"), ""); + th.text(value); + tr.appendChild(th); + } + thead.appendChild(tr); + table.appendChild(thead); + // Add data rows + Element tbody = new Element(Tag.valueOf("tbody"), ""); + for (int i = 1; i <= sheet.getLastRowNum(); i++) { + Element trData = new Element(Tag.valueOf("tr"), ""); + for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) { + Cell cell = sheet.getRow(i).getCell(j); + String value = ""; + if (cell.getCellTypeEnum() == CellType.NUMERIC) { + value = String.valueOf(cell.getNumericCellValue()); + } else { + value = cell.getStringCellValue(); + } + Element td = new Element(Tag.valueOf("td"), ""); + td.text(value); + if (j < 2) { + td.attr("align", "left"); + } + trData.appendChild(td); + } + tbody.appendChild(trData); + } + table.appendChild(tbody); + // Convert to HTML + Document doc = new Document(""); + doc.appendChild(table); + String html = doc.outerHtml(); + // Write to file + FileWriter writer = new FileWriter(new File("D:\\uploads\\output.html")); + writer.write(html); + writer.close(); + } catch (Exception ex) { + System.out.println(ex.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/service/input.xlsx b/src/main/java/com/example/demo/service/input.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..c4c4a08138fb8a947024d2d67ac3a5224dd5465f GIT binary patch literal 17736 zcmeHPTW=&s6?RAf5fnlo4^a?OYb1C8+2bBx);6|-J+}AawRhr~%?&|R(_J&u>z?jT zcaJX-4@rPzLs&NCjz~fxt`e|Wl!R<9!qvX3@kT+bP z4s0~-$(!JhwnqKHa-FH1J}{WmsAIUM<vAdiDMGmb?8)XlLon)rmwkr z((opb39_SpXwg6ePEIgkcvHDX7zm>3|xp1*DBXqh#&-*Qk_2F>NCOOhdO*!~wC`dD8d zemD)Lh!cfa)Z+FNO>}!&YV0%2|8biSbi`)xfV{WQ6qGHEwJteQx$Srl>`*oP36{z zZxJU1wg}^2Slaf2(4W55^;ZL|BbdwsfM>E~8k=LInY)I90SFzx*Ih7kkC|8ld8m_3nY zq}`fRk6Z{7g0?W)U0e0Yi!dQ*yCsd>NKgEzOK;F4Kf*+hwML$#C$7}3z>zEIJ^PX# zITI!#^Ax&NapX^W;!wNm3FbH$TUa!jgE`Oir0=e45D9gNV+JN>z)N7jg%Jz~is_5z zUfX{9{MN&#U_gQ}WeC))jM_Q#{LaJoA1=wDq72%8{OtCrGb(6K0X=xn*3-X!>*eom zpMQA!_s_`;=N0Uur!Kz!^v;W~?Y#C=8Mq(=-#YWht)D-zbK!}d=kAq(2W8+kt9|t=kDp$2V`s!X3)oA z5}TH7L59o>>izIf#}A}s?7=pyTz*I{SEvswr7HI$N_kuLiwdAHG(0kTU~F6#;;4f9 zieAj?2WRv8SM-DB@*F-Zv*nq}j7+v9lW|X0TCm&&=9r9un&o;*O1WHmvr<}7s8^J7 zY>UjHEF-pm{rvVbZ*Y%}fs!u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