Java-excel Export

Source: Internet
Author: User

private void Export_button_actionevent (ActionEvent e) throws IOException, WriteException {
TODO auto-generated Method Stub
try{
Filechooser.setfileselectionmode (jfilechooser.files_and_directories);

int Returnval=filechooser.showsavedialog (filechooser);//Popup Save Form

String Filepath=null;
if (returnval==jfilechooser.approve_option)
{
Filepath=filechooser.getselectedfile (). GetAbsolutePath () + ". xls";//Set Export format
System.out.println ("FilePath:" +filepath);
}
Writableworkbook Book=workbook.createworkbook (New File (FilePath));
Writablesheet sheet= book.createsheet ("SheetName", 0); Create sheet
for (int col = 0;col<defaulttablemodel.getcolumncount (); col++) {
try{
Sheet.addcell (New Label (Col,0,defaulttablemodel.getcolumnname (col))); Write column headings to excel
}catch (Exception E2) {
E2.printstacktrace ();
}
}
Writing data to Excel
for (int j = 0; J < Defaulttablemodel.getcolumncount (); j + +)
{
System.out.println ("Number of columns:" +defaulttablemodel.getcolumncount ());

for (int i = 0; i < stocker_name.size (); i++)
{
System.out.println ("Number of rows:" +defaulttablemodel.getrowcount ());

try{
Sheet.addcell (New Label (J,i+1,defaulttablemodel.getvalueat (i, J). ToString ())); Writing content to Excel
System.out.println ("+i+" line + "+j+" column: "+defaulttablemodel.getvalueat (i, J). ToString ());
}catch (Exception E1)
{
E1.printstacktrace ();
}

}
}
Book.write ();
Book.close ();
Joptionpane.showmessagedialog (NULL, "file export succeeded");
}catch (Exception E1) {
E1.printstacktrace ();
}

}

Using the Jxl.jar Package

Java-excel Export

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.