如何从java输出到excel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从java输出到excel相关的知识,希望对你有一定的参考价值。

POI我当然知道啦,详细点

用JAVA程序,读取或者写入excel文件,通过用jxl或者poi,下面是我给你写的例子。分别是用jxl读写excel文件,用poi读写excel文件。希望对你有帮助。(需要下载jxl和poi的jar包)

package util.excel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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 ExcelUtil

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
String outFile = "D:/workspace/JavaStudy/src/util/excel/test.xls";
ExcelUtil.writeExcelByJXL(outFile, null);


/**
*
* @title: readExcelByJXL
* @description: 通过jxl读取excel文件
* @author yu ren tian
* @email yurentian@163.com
* @param excelFile
* @return
* @throws IOException
*/
private static List readExcelByJXL(String excelFile) throws IOException
List rtn = new ArrayList();
FileInputStream fileInputStream = null;
try
fileInputStream = new FileInputStream(excelFile);
Workbook excelWorkBook = Workbook.getWorkbook(fileInputStream);
Sheet sheet = excelWorkBook.getSheet(0);
int m = sheet.getRows();
int n = sheet.getColumns();
for (int i = 1; i < m; i++)
Map map = new HashMap();
for (int j = 0; j < n; j++)
Cell cell = sheet.getCell(j, i);
String cellContent = cell.getContents();
switch (j)
case 0:
map.put("studentName", cellContent);
break;
case 1:
map.put("Chinese", cellContent);
break;
case 2:
map.put("Math", cellContent);
break;
case 3:
map.put("English", cellContent);
break;
case 4:
map.put("assess", cellContent);
break;


rtn.add(map);

catch (Exception e)
e.printStackTrace();
finally
if (null != fileInputStream)
fileInputStream.close();

return rtn;



/**
*
* @title: writeExcelByJXL
* @description: 通过jxl写入excel文件
* @author yu ren tian
* @email yurentian@163.com
* @param outFile
* @param list
* @throws IOException
*/
private static void writeExcelByJXL(String outFile, List list)
throws IOException
WritableWorkbook wwb;
FileOutputStream fos;
try
fos = new FileOutputStream(outFile);
// wwb = Workbook.createWorkbook(file);
wwb = Workbook.createWorkbook(fos);
WritableSheet sheet = wwb.createSheet("test", 0);
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLUE);
WritableCellFormat wcf = new WritableCellFormat(wf);
//wcf.setBackground(Colour.GREEN);
wcf.setBackground(new CustomColor(11, "", 0, 0, 0));
for (int i = 0; i < 10; i++)
Label label = new Label(i, 0, i + "", wcf);
sheet.addCell(label);


wwb.write();
wwb.close();
fos.close();
catch (Exception e)
e.printStackTrace();



/**
*
* @title: readExcelByPOI
* @description: 通过poi读取excel文件
* @author yu ren tian
* @email yurentian@163.com
* @param excelFile
* @return
* @throws IOException
*/
private static List readExcelByPOI(String excelFile) throws IOException
List rtn = new ArrayList();
FileInputStream fin = null;
try
fin = new FileInputStream(excelFile);
POIFSFileSystem fs = new POIFSFileSystem(fin);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int m = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
int n = 5;
for (int i = 1; i < m; i++)
Map map = new HashMap();
for (int j = 0; j < n; j++)
HSSFCell cell = sheet.getRow(i).getCell((short) j);
int type = cell.getCellType();
String cellContentString = null;
double cellContentDouble = 0;
if (type == 1)
cellContentString = cell.getRichStringCellValue()
.getString();
System.out.println("cellContentString="
+ cellContentString);
else if (type == 0)
cellContentDouble = cell.getNumericCellValue();
System.out.println("cellContentDouble="
+ cellContentDouble);

System.out.println("j=" + j);
switch (j)
case 0:
map.put("studentName", cellContentString);
break;
case 1:
map.put("Chinese", new Double(cellContentDouble));
break;
case 2:
map.put("Math", new Double(cellContentDouble));
break;
case 3:
map.put("English", new Double(cellContentDouble));
break;
case 4:
map.put("assess", cellContentString);
break;



catch (Exception e)
e.printStackTrace();
finally
if (fin != null)
fin.close();

return rtn;



/**
*
* @title: writeExcelByPOI
* @description: 通过poi写入excel
* @author yu ren tian
* @email yurentian@163.com
* @param outFile
* @param list
* @throws IOException
*/
private static void writeExcelByPOI(String outFile, List list)
throws IOException
FileOutputStream fos = new FileOutputStream(outFile);
HSSFWorkbook wb = new HSSFWorkbook();
for (int sheetCount = 0; sheetCount < 5; sheetCount++)
HSSFSheet sheet = wb.createSheet("组织" + (sheetCount + 1));
for (int rowCount = 0; rowCount < 10; rowCount++)
for (int columnCount = 0; columnCount < 10; columnCount++)
HSSFRow row = sheet.createRow(rowCount);
HSSFCell cell = row.createCell(new Short(columnCount + ""));
HSSFRichTextString richTextString = new HSSFRichTextString(
"行=" + rowCount + " 列=" + columnCount);
cell.setCellValue(richTextString);



wb.write(fos);

参考技术A 如果是JSP页面要导出成excel,依靠javascript就可以实现了,具体你试试上面的代码
<input type="button" value="保存为 Excel">
<script language="javascript">
function AllAreaExcel()
var title;
title=document.getElementsByTagName("table")[0].childNodes.item(0).childNodes(0).childNodes(0).innerText;
alert(title);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
//从excel的第5行开始插入
oSheet.Range("A5").select;
oWB .Worksheets(1).Activate;
oSheet.Cells(3,1).Value=title; //在第3行插入报表头
oWB.Worksheets(1).Range("A3:I3").merge(); // 合并单元格区域 A3:I3
oWB.Worksheets(1).Range("A3:I3").HorizontalAlignment=3; //居中对齐A3:I3
var sel=document.body.createTextRange();
sel.moveToElementText(table1); //table 的ID值
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;

</script>

java中输入输出流如何把数据输出为Excel表格形式

参考技术A

实现代码如下:

import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

publicclass CreateCells

publicstaticvoid main(String[] args)

throws IOException

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象

// Create a row and put some cells in it. Rows are 0 based.

HSSFRow row = sheet.createRow((short)0);//建立新行

// Create a cell and put a value in it.

HSSFCell cell = row.createCell((short)0);//建立新cell

cell.setCellValue(1);//设置cell的整数类型的值

// Or do it on one line.

row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值

row.createCell((short)2).setCellValue("test");//设置cell字符类型的值

row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值

HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式

HSSFCell dCell =row.createCell((short)4);

dCell.setCellValue(new Date());//设置cell为日期类型的值

dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式

HSSFCell csCell =row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断

csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

Java是由Sun Microsystems公司推出的Java面向对象程序设计语言(以下简称Java语言)和Java平台的总称。由James Gosling和同事们共同研发,并在1995年正式推出。Java最初被称为Oak,是1991年为消费类电子产品的嵌入式芯片而设计的。1995年更名为Java,并重新设计用于开发Internet应用程序。

用Java实现的HotJava浏览器(支持Java applet)显示了Java的魅力:跨平台、动态Web、Internet计算。从此,Java被广泛接受并推动了Web的迅速发展,常用的浏览器均支持Javaapplet。另一方面,Java技术也不断更新。Java自面世后就非常流行,发展迅速,对C++语言形成有力冲击。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。2010年Oracle公司收购Sun Microsystems。

以上是关于如何从java输出到excel的主要内容,如果未能解决你的问题,请参考以下文章

如何在导出到 Excel 文件之前从 Python DataFrame 设置多行样式

如何将此功能从 Excel 转换为 Open Office?

如何将 PowerShell 树输出到文本或 Excel 文件

如何将jsp 中的数据导入到excel表格 中

如何将excel的数据输出到文本文件中并设定长

如何将R语言中的表格数据输出为Excel文件