POI reads EXCEL and copies sheet

Source: Internet
Author: User

 

Java code

/*************************************** *****************************

*

* (C) Copyright ISFnet Japan, Ltd. 2011 All rights reserved.

*

**************************************** ****************************/

Package excel;

 

Import java. io. FileInputStream;

Import java. io. FileNotFoundException;

Import java. io. FileOutputStream;

Import java. io. IOException;

 

Import org. apache. poi. hssf. usermodel. HSSFSheet;

Import org. apache. poi. hssf. usermodel. HSSFWorkbook;

Import org. apache. poi. poifs. filesystem. POIFSFileSystem;

Import org. apache. poi. ss. usermodel. Cell;

Import org. apache. poi. ss. usermodel. Row;

Import org. apache. poi. ss. util. CellRangeAddress;

 

/**

* <P>

*

* @ Author ISFnet DALIAN muzongqin

* @ Since 2011/08/10

* @ Version 1.0

*/

Public class CopyExcel {

 

/**

*

*

*

* @ Param args

* Void

* @ Throws IOException

* @ Throws FileNotFoundException

*/

Public static void main (String [] args) throws FileNotFoundException, IOException {

// You can see the following information.

Final String INVOICE_FILE = "C: \ work \ jyuchu \ 3_ .......xls ";

// When there are too many attempts, there are too many attempts.

POIFSFileSystem filein = new POIFSFileSystem (new FileInputStream (INVOICE_FILE ));

// When there are too many attempts, there are too many attempts.

HSSFWorkbook wb = new HSSFWorkbook (filein );

// When there are too many threads, there are too many threads.

HSSFSheet sheet1 = wb. getSheet (" ");

HSSFSheet sheet2 = wb. createSheet ("Notify supervisor 1 ");

Sheet2 = copySheet (sheet1, sheet2 );

FileOutputStream fileOut = new FileOutputStream ("d: \ test1.xls ");

Wb. write (fileOut );

FileOut. close ();

 

}

 

Private static HSSFSheet copySheet (HSSFSheet sheetFrom, HSSFSheet sheetTo ){

 

// Initial Stage

CellRangeAddress region = null;

Row rowFrom = null;

Row rowTo = null;

Cell cellFrom = null;

Cell cellTo = null;

// When combined, when combined

For (int I = 0; I <sheetFrom. getNumMergedRegions (); I ++ ){

Region = sheetFrom. getMergedRegion (I );

If (region. getFirstColumn ()> = sheetFrom. getFirstRowNum ())

& (Region. getLastRow () <= sheetFrom. getLastRowNum ())){

SheetTo. addMergedRegion (region );

}

}

 

// Please wait until then

For (int intRow = sheetFrom. getFirstRowNum (); intRow <sheetFrom. getLastRowNum (); intRow ++ ){

RowFrom = sheetFrom. getRow (intRow );

RowTo = sheetTo. createRow (intRow );

If (null = rowFrom)

Continue;

RowTo. setHeight (rowFrom. getHeight ());

For (int intCol = 0; intCol <rowFrom. getLastCellNum (); intCol ++ ){

// Adjust the bandwidth and then adjust the bandwidth

SheetTo. setdefacolumcolumnstyle (intCol, sheetFrom. getColumnStyle (intCol ));

SheetTo. setColumnWidth (intCol, sheetFrom. getColumnWidth (intCol ));

CellFrom = rowFrom. getCell (intCol );

CellTo = rowTo. createCell (intCol );

If (null = cellFrom)

Continue;

// When there are too many threads, there are too many other threads.

CellTo. setCellStyle (cellFrom. getCellStyle ());

CellTo. setCellType (cellFrom. getCellType ());

// Too many contents

If (null! = CellFrom. getStringCellValue ()&&! "". Equals (cellFrom. getStringCellValue (). trim ()))

CellTo. setCellValue (cellFrom. getStringCellValue ());

}

}

// Configure your instances

SheetTo. setDisplayGridlines (false );

// Set the Excel worksheet.

SheetTo. setZoom (80,100 );

// Please wait until then.

Return sheetTo;

 

}

 

}

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.