In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Java Hutool tools to achieve CAPTCHA generation and import and export of Excel files". In daily operation, I believe that many people have doubts about how to use Java Hutool tools to achieve CAPTCHA generation and Excel file import and export. The editor consulted all kinds of materials and sorted out simple and useful operation methods. Hope to answer the question of "how to use Java Hutool tools to generate CAPTCHA and import and export Excel files" will be helpful! Next, please follow the editor to study!
1. Brief introduction of Hutool tools
HuTool tools (confused tools), third-party plug-in tools, simplify the operation, is a domestic product, the interface is simple and easy to understand, more user-friendly. (it may be often used at work, so it is recommended to keep it.)
Hutool is a small and comprehensive Java tool class library, encapsulated by static methods, reduce the learning cost of related API, improve work efficiency, make Java have functional language-like elegance, so that Java language can also be "sweet".
2. Dependence of Hutool
The maven project can add the following dependencies to pom.xml:
Cn.hutool hutool-all 4.6.33, CAPTCHA tool @ Test public void hutoolCodeTest () throws FileNotFoundException {/ * *, * 1, Create a CAPTCHA: * CAPTCHA captcha ['k æ pt verification codes] * line: line * parameter description * width: width * height: height * codeCount: number of characters * lineCount: number of interference lines * / LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha 40, 4, 4) / / 2. Get the real code value of the generated verification code String code = lineCaptcha.getCode (); System.out.println (code); / / 3. Output the CAPTCHA image to lineCaptcha.write (new FileOutputStream ("D:/text.png")) under the root directory of D disk;}
4. Excel tool
POI relies on import, otherwise an error is reported
You need to add dependency of 'poi-ooxml' to your project, and version > = 3.17
Org.apache.poi poi-ooxml 3.17 org.apache.poi poi-ooxml-schemas 3.17
(1) Exel export
Test public void HutollDownLoadExcelTest () throws FileNotFoundException {/ / 1. Create an Excel write tool Writer ExcelWriter writer = ExcelUtil.getWriter (true); / / 2. Simulated List data List list = new ArrayList (); list.add (1); list.add (2); list.add (3); / / 3. Output list data to writer.write (list) in excel; / / 4. Save the excel file after filling the data in the d disk file. Writer.flush (new FileOutputStream ("d:/id.xlsx"));}
(2) detailed explanation of excel export
Entity class
Public class User {private String id; private String name; private Integer age
Test class
/ / 1. Create an Excel write tool Writer ExcelWriter writer = ExcelUtil.getWriter (true); / / 2. Set aliases writer.addHeaderAlias ("id", "number"); writer.addHeaderAlias ("name", "first name"); writer.addHeaderAlias ("age", "age"); writer.addHeaderAlias ("birth", "birthday"); / / 3. First output a row of headings (parameter 1 is the number of columns across, starting with 0. Parameter 2 is the title string) writer.merge (3, "title"); / / 4. Simulate list collection data List users = getUsers (); / / 5. Export users data to an excel file. Writer.write (users,true); / / true represents and outputs the title. / / 6. Save the excel file after filling the data in the d disk file. Writer.flush (new FileOutputStream ("D:/users.xlsx"));} private List getUsers () {List list = new ArrayList (); list.add (new User ("1001", "Zhang San", 18 new Date ()); list.add (new User ("1002", "Zhang San", 18 new Date ()); list.add (new User ("1003", "Zhang San", 18 new Date () List.add (new User ("1004", "Zhang San", 18heroine new Date ()); return list;}
(2) Excel import
Test public void HutollUploadExcelTest () throws FileNotFoundException {/ / 1. Create an Excel reading tool reader ExcelReader reader = ExcelUtil.getReader (new FileInputStream ("D:/users.xlsx")); / / 2. Sets the alias of the read data and the attribute correspondence of the encapsulated entity. Reader.addHeaderAlias ("number", "id"); reader.addHeaderAlias ("name", "name"); reader.addHeaderAlias ("age", "age"); reader.addHeaderAlias ("birthday", "birth"); / * reader reads the excel file data parameter description headerRowIndex: which line is the mapped data title, starting with 0. StartRowIndex: the actual read data starts from which line, counting from 0. Class: what type of object is the read data encapsulated into * / List users = reader.read (1, 2, User.class); / / output the returned users collection System.out.println (users); / / output the size of the users. System.out.println (users.size ());}
At this point, the study on "how to use Java Hutool tools to generate CAPTCHA and import and export Excel files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.