In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The editor today takes you to understand how the mutation of Office file format promotes the perfect integration of Java and Office. The knowledge points in this article are introduced in great detail. Friends who think it is helpful can browse the content of the article together with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Follow the editor to learn in depth how the mutation of Office file format promotes the perfect integration of Java and Office.
Sudden change in Office file format, leading to better integration of Java and Office
Office is an office software that everyone is very familiar with. Its powerful text and table processing ability is almost unbeatable. In addition to being used in traditional table processing, Excel is often "grasped" by programmers to act as the front end of the report system. Such as the Java Web program we use, generate an Excel file with report data in the background, and then send the file to the foreground browser (must be IE), so that the Excel file can be embedded in IE.
Processing reports in this way can not only make use of the powerful table processing function of Excel, but also save programmers a lot of trouble. But there is a limit to this. That is, when the Java of the server generates Excel reports, it generally does not use COM to call the Excel component, but generates the Excel file directly according to the binary format of the Excel file. However, because Office is commercial software, Microsoft does not fully disclose the file format of Office (even if it is public, it is not the latest version), which brings some difficulties to generate Excel files. Although some relatively new Office documents can be saved in XML format, this file format is only a crude alternative, and the front-end IE does not recognize Excel documents in XML format. Maybe Microsoft is aware of the problem and solved it completely in the new Office2007.
In previous versions of Office, there were a lot of problems in dealing with Office documents without Office for legal or technical reasons. Perhaps because of the pressure on Microsoft from open source, or for other reasons, Microsoft has done a completely different treatment of the Office2007 format than before. Previous Office documents were in 100% binary format. Third-party tools are very inconvenient to operate, and Office2007 is based on XML format as a whole, which is not to say that Office2007 documents can be saved in XML format. Instead, the default document format for Office2007 is XML (docx for Word, xlsx for Excel, etc.). Some people may find it strange that when you open docx with a text editor, it is still in binary format, not XML. In fact, docx is not an ordinary XML format, of course, it is not just a XML file, docx is essentially a zip file with a series of xml, directories and other files. If we change docx to zip. You can unlock it with software such as winzip (from this we can see that the x in docx means XML).
The XML in the directory is based on the OpenXML format, a specification that Microsoft has provided and submitted to ECMA, so it has become a public standard and we are free to use them. Because Office2007 documents all use compressed xml format, only languages that support reading zip format and xml format can manipulate Office2007 documents, and of course, Java is no exception. It will be easier to use Java and Office to jointly manipulate reports. In this article, I will demonstrate how to use Java to manipulate Office2007 documents, taking word2007 as an example.
We save this document as test.docx. Be careful not to save to a backward compatible word document format, nor to Office2003 or better Office WordML format. This document will be in a compressed zip format, if you change test.docx to test.zip.
From the unraveling of the file structure above, you can clearly understand the save structure of test.docx. In Java we can use the java.util.zip package to unlock the test.docx. From this directory structure, we can easily guess that the main contents of the document are saved in document.xml. Other xml files will hold different information. For example, font information will be saved in fontTable.xml, while Office themes will be saved in theme.xml and theme1.xml.
Let's use Java to manipulate this file. First, we use JUnit4 to determine whether test.docx exists and can be read and written. The code is as follows:
@ Test public void verifyFile ()
... {
AssertTrue (new File ("test.docx") .exists ())
AssertTrue (new File ("test.docx") .canRead ())
AssertTrue (new File ("test.docx") .canWrite ())
}
The following code will simply verify
Java.util.zip.ZipFile
Whether the class can be opened
Test.docx
.
@ Test public void openFile () throws IOException, ZipException
... {
ZipFile docxFile = new ZipFile (new File ("test.docx"))
AssertEquals (docxFile.getName (), "test.docx")
}
After testing, ZipFile can fully operate test.docx. It seems that many people can't wait, so let's read the data from test.docx. You should first open the document.xml file. The code is as follows:
Test public void listContents () throws IOException, ZipException
... {
Boolean documentFound = false
ZipFile docxFile = new ZipFile (new File ("test.docx"))
Enumeration entriesIter = docxFile.entries ()
While (entriesIter.hasMoreElements ())
... {
ZipEntry entry = entriesIter.nextElement ()
If (entry.getName () .equals ("document.xml")
DocumentFound = true
}
AssertTrue (documentFound)
}
But running the above code will throw an exception, which seems to indicate that document.xml does not exist, but in fact it is not. Instead, ZipFile API needs a full file or directory name, so you need to change the above path to word/document.xml.
Next we will pass the
ZipFile
Get one.
ZipEntry
Object and look at it through this object
Xml
What is in, the code is as follows:
@ Test public void getDocument () throws IOException, ZipException
... {
ZipFile docxFile = new ZipFile (new File ("test.docx"))
ZipEntry documentXML = docxFile.getEntry ("word/document.xml")
AssertNotNull (documentXML)
}
Thank you for your reading, the above is "Office file format mutation how to promote Java and Office more perfect integration" all the content, learn friends hurry up to operate it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
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.