java achieve excel import export (1) (transfer)

Java data into Excel

package com.bossy.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelOpt (
/ **
* Generate a Excel file jxl
* @ Param fileName the file name to generate the Excel
* @ Jxl.jar version: 2.6
* /
public static void writeExcel (String fileName) (
WritableWorkbook wwb = null;
try (
/ / First of all, to use the Workbook class factory method to create a writable workbook (Workbook) object
wwb = Workbook.createWorkbook (new File (fileName));

) Catch (IOException e) (
e.printStackTrace ();
)
if (wwb! = null) (
/ / Create a writable sheet
/ / Workbook for createSheet method has two parameters, the first is the name of the worksheet, the second is a worksheet in the workbook's location
WritableSheet ws = wwb.createSheet ("sheet name", 0);

/ / Start adding the following cell
for (int i = 0; i <10; i + +) (
for (int j = 0; j <5; j + +) (
/ / It should be noted that, in Excel, the first parameter, said column, said second row
Label labelC = new Label (j, i, "This is the first" + (i +1) + "at line" + (j +1) + "column");
try (
/ / Will be added to the cells generated worksheet
ws.addCell (labelC);
) Catch (RowsExceededException e) (
e.printStackTrace ();
) Catch (WriteException e) (
e.printStackTrace ();
)

)
)

try (
/ / Write the file from memory
wwb.write ();
/ / Close the resources, the release of memory
wwb.close ();
) Catch (IOException e) (
e.printStackTrace ();
) Catch (WriteException e) (
e.printStackTrace ();
)
)
)

/ **
* Generate a Excel file POI
* @ Param inputFile input template file path
* @ Param outputFile the input file stored on the server path
* @ Param dataList data to be exported
* @ Throws Exception
* @ Roseuid:
* /

public static void exportExcelFile (String inputFile, String outputFile, List dataList) throws Exception (
/ / Use the template file structure poi
POIFSFileSystem fs = new POIFSFileSystem (new FileInputStream (inputFile));
/ / Create a template worksheet
HSSFWorkbook templatewb = new HSSFWorkbook (fs);
/ / Get the template directly to the first sheet object
HSSFSheet templateSheet = templatewb.getSheetAt (1);
/ / Get the first sheet template first line of the object in order to get the template style
HSSFRow templateRow = templateSheet.getRow (0);

/ / HSSFSheet timplateSheet = templatewb.getSheetAt (1);
/ / Get the total number of columns Excel file
int columns = templateSheet.getRow ((short) 0)
. GetPhysicalNumberOfCells ();
/ / Debug.println ("columns is:" + columns); //=========================
/ / Create an array of styles
HSSFCellStyle styleArray [] = new HSSFCellStyle [columns];

/ / One-time style to create all the columns on the array
for (int s = 0; s <columns; s + +) (
/ / Get an array of instances
styleArray [s] = templatewb.createCellStyle ();
)
/ / Loop for each cell were assigned
/ / Positioning line
for (int rowId = 1; rowId <dataList.size (); rowId + +) (
/ / Turn to take the first rowId line data for each data valueList
List valueList = (List) dataList.get (rowId - 1);
/ / Location column
for (int columnId = 0; columnId <columns; columnId + +) (
/ / Turn out the corresponding column values with colunmId
/ / Value of each cell
String dataValue = (String) valueList.get (columnId);
/ / Remove colunmId out of style
/ / Template style for each column
HSSFCellStyle style = styleArray [columnId];
/ / Get template first colunmId target cell column
/ / Template object cell
HSSFCell templateCell = templateRow.getCell ((short) columnId);
/ / Create a new object rowId trekking
/ / New row object
HSSFRow hssfRow = templateSheet.createRow (rowId);
/ / Create a new column of cells rowId row columnId object
/ / New cell object
HSSFCell cell = hssfRow.createCell ((short) columnId);
/ / If the corresponding template cell style non-locking
if (templateCell.getCellStyle (). getLocked () == false) (
/ / Set this out in style non-locking
style.setLocked (false);
/ / Set the cell on the new
cell.setCellStyle (style);
)
/ / Otherwise the style is locked
else (
/ / Set this out in style for the lock
style.setLocked (true);
/ / Set the cell on the new
cell.setCellStyle (style);
)
/ / Set encoding
cell.setEncoding (HSSFCell.ENCODING_UTF_16);
/ / Debug.println ("dataValue:" + dataValue);
/ / Set the value of unity for the String
cell.setCellValue (dataValue);
)
)
/ / Set the input stream
FileOutputStream fOut = new FileOutputStream (outputFile);
/ / Write out the contents of the template file
templatewb.write (fOut);
fOut.flush ();

/ / End of the operation, close the file
fOut.close ();

)

/ **
* Export data to XLS format
* @ Param fos generate Excel file Path
* @ Param bo the data to be imported
* /
public static void writeExcelBo (String fos, java.util.List ve)
(
jxl.write.WritableWorkbook wwb;
try
(
wwb = Workbook.createWorkbook (new File (fos));
jxl.write.WritableSheet ws = wwb.createSheet ("listing book", 10);
ws.addCell (new jxl.write.Label (0, 1, "title"));
ws.addCell (new jxl.write.Label (1, 1, "author"));
ws.addCell (new jxl.write.Label (2, 1, "price"));
ws.addCell (new jxl.write.Label (3, 1, "Press"));
int bookSize = ve.size ();
BookVO book = new BookVO ();
for (int i = 0; i <bookSize; i + +)
(
book = (BookVO) ve.get (i);
ws.addCell (new jxl.write.Label (0, i + 2, "" + book.getBookName ()));
ws.addCell (new jxl.write.Label (1, i + 2, book.getBookAuthor ()));
ws.addCell (new jxl.write.Label (2, i + 2, "" + book.getBookPrice ()));
ws.addCell (new jxl.write.Label (3, i + 2, book.getBookConcern ()));

)
/ / Jxl.write.WritableFont wfc =
/ / New jxl.write.WritableFont (WritableFont.ARIAL, 255, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
/ / Jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat (wfc);
ws.addCell (new jxl.write.Label (0, 0, "2007 Last Coming soon listed book !"));
wwb.write ();
/ / Close the Excel workbook object
wwb.close ();
) Catch (IOException e) (
) Catch (RowsExceededException e) (

) Catch (WriteException e) (
)
)

public static void main (String [] args) (
writeExcel ("c: \ \ Test Test Excel.xls");
System.out.println ("OK");
ArrayList list = new ArrayList ();

for (int i = 0; i <10; i + +) (
BookVO book = new BookVO ();
book.setBookName ("WebWork in action +" + i);
book.setBookAuthor ("Tang Yong +" + i);
book.setBookPrice ("39 Element +" + i);
book.setBookConcern ("Fei Sike Technology +" + i);
list.add (book);
)

writeExcelBo ("c: \ \ listed book. xls", list);
System.err.println ("Book OK !!!");

)

)

分类:Java 时间:2010-05-02 人气:505
分享到:
blog comments powered by Disqus

相关文章

  • 基于 apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件 2014-12-15

    package com.genomics.ib.item.control; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.common

  • JAR package for Java, or the path to read the resource file 2010-07-26

    Here are divided into two specific: The first: the resource file for the general suffix file The second: Resource file for picture files First] [NO1 Use this line of code can get class path of the root class String path = Thread.currentThread (). Get

  • jxl.jar Package Introduction / java operating excel jxl.jar Download 2010-10-04

    Download: http://www.andykhan.com/jexcelapi/ current maximum version is 2.6. Real Download: http://www.andykhan.com/jexcelapi/download.html On its website features the following description: ● support for all versions of Excel 95-2000 ● ● generate su

  • Some rights java modifiers, private / friendly / protected / public 2010-05-29

    Begin testing the following: A Test class, as was access to the class, private not say, can access only its own, public is able to access, this is mainly to distinguish between friendly and protected New two classes, Test1 and Test2 to visit Test, Te

  • Internal class version of Java HelloWorld 2010-06-13

    Previous study, when not within the class how to see and feel this thing rarely used, on the first skip. Now found time to pick up come. Work, are gradually used up. So, write a HelloWorld internal class version. Initially want to write is: Using Jav

  • Java was a little thing 3 - static method 2010-09-19

    In Java, static methods of use is another common, but of a static object and the method has the advantage Needless to say, you must also know. You will ask him where I want to say? I asked: Do you know Why static method can be rewritten? Perhaps you

  • 如何用jar命令对java工程进行打包 2014-03-05

    有时候为了更方便快捷的部署和执行Java程序,要把java应用程序打包成一个jar包.而这个基础的操作有时候也很麻烦,为了方便java程序员们能够方便的打包java应用程序,下面对jar命令进行介绍,并举出几个简单例子针对不同情况进行打包. 一. jar命令用法: 在cmd命令窗口下输入jar,回车,就会提示改命令的用法: 二.例子(这里介绍的都是生成双击可执行的jar包): 1.首先介绍如何在命令行下执行打包程序. 1.1.没有包结构的最简单的工程. (1)在c:盘下新建文件"HelloWor

  • JAVA reflection, reduce the use of JDBC difficulty 2010-04-24

    Class 1: package com.demo.database; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Result

  • Java code to generate Excel table 2010-06-24

    Inscription: This is my letter to Yu Yi Cheng's second year, I was transferred to the data sector, now I am endless, and intertwine between data and Excel. 1. We first define these three classes DataItem class, said that the figures within a cell pac

  • java反射技术详解 2012-05-05

    反射就是把Java类中的各种成分映射成相应的java类. Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说"自审",并能直接操作程序的内部属性. 1.检测类: 1.1 reflection的工作机制 考虑下面这个简单的例子,让我们看看 reflection 是如何工作的. import java.lang.reflect.*; public class DumpMethods { public static void ma

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.042 (s). 11 q(s)