Poi Excel operations

Source: Internet
Author: User

In the past two days, I used poi to generate an Excel file. I looked at the example provided by my boss and started to write it. The writing was still smooth, and the data could be exported to the Excel file. The layout would be very troublesome, all

All operations are requiredProgramFor a while, I thought this would not work. I should create a template, So I extracted the Excel format and put it into a new Excel file.

Open the template file, insert data from the template file, but save the data to another Excel file during storage, which saves a lot of layout trouble.

. Of course, this also has some problems:
What if there are multiple sheets? A: If you need multiple sheets, you can create several more template sheets in the template.
What if I have created many templates, but they are not useful? A: You can delete more than one sheet before saving it.
The above two problems have been solved. It should be said that there is no problem, but a new problem occurs. Excel can be generated, but after opening the sheet, it cannot be switched. If everything is changed, an error is returned. Half checked

I don't know what the reason is. Ask the boss to show it to me. He will tell me after reading it.CodeNo problem. The problem is that the last added sheet must be selected in an Excel template.

Sheet. For example, you have nine sheets from sheet1 to sheet9, so you have to set sheet9 to select and save it. In this way, there is no problem with excle everywhere Based on the template.

Public   Static   Void Createexcelfromtemplate (string templatefile, string SaveFile, arraylist exceldata)
{
// Read template Excel
Hssfworkbook tempworkbook =   New Hssfworkbook ( New Fileinputstream (templatefile ));

// Process Excel
//

Tempworkbook = Deletesheet (tempworkbook );
// Save Excel
Generateexcel (tempworkbook, SaveFile );
}
/**/ /*Generate Excel documents by hssfworkbook
*
**/
Public   Static   Void Generateexcel (hssfworkbook WB, string SaveFile) {
Try   { // Create an output file stream
Fileoutputstream fout =   New Fileoutputstream (SaveFile );
// Save the corresponding Excel Workbook
WB. Write (fout );
Fout. Flush ();
// Operation ended. close the file.
Fout. Close ();
}   Catch (Exception E) {
E. printstacktrace ();//To change body of catch statement use file | Settings | file templates.
}
}

// Delete more sheet
Public   Static Hssfworkbook deletesheet (hssfworkbook WB) {

Int Totalsheets = WB. getnumberofsheets () -   1 ;
While ( 0   <= Totalsheets) {
If (WB. getsheetname (totalsheets). lastindexof ( " Sheet " ) =   0 ) {
WB. removesheetat (totalsheets );
Totalsheets--;
}   Else   {
Totalsheets--;
}
}
Return WB;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.