问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

javaexcel

发布网友 发布时间:2023-06-20 01:50

我来回答

2个回答

热心网友 时间:2023-10-03 05:51

用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);
}
}

热心网友 时间:2023-10-03 05:51

如果是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>

热心网友 时间:2023-10-03 05:51

用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);
}
}

热心网友 时间:2023-10-03 05:51

如果是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>
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
贷款记录在征信保留几年? 安徽徽商城有限公司公司简介 安徽省徽商集团新能源股份有限公司基本情况 安徽省徽商集团有限公司经营理念 2019哈尔滨煤气费怎么有税? 快手删除的作品如何恢复 体育理念体育理念 有关体育的格言和理念 什么是体育理念 万里挑一算彩礼还是见面礼 北京和路元医疗技术有限公司怎么样? 北京坤泰元和科技发展有限公司卓艺医疗美容诊所怎么样? 石家庄哪些路口拍限行 北京亦正和元信息咨询中心是正规公司吗?我前几天向这个公司投了份简历,今天信息通知我去面试。 北京中和盛元科技有限公司怎么样? 北京有限元科技有限公司电话是多少? 北京元生和科技有限公司怎么样? 广州元下田村属于限行区域吗? 北京和熙元科技发展有限公司怎么样? 北京神州元和科技有限公司怎么样? 田村路有限行摄像头吗 北京顺和元科技有限公司怎么样? 福禄克哪里能买到正品 fluke测试光缆结果中的不适用是什么意思 福禄克万用表从沈阳发货是正品吗 福禄克序列号查询真伪 专家释疑精液为何是黄色果冻状 计算机的系统组成都包括哪些 墓邪的2墓邪闽南花谷 长沙118路公交车所有经过站点的名字是什么? 梦见龙吃鸡的预兆 梦见属龙男属鸡女的预兆 梦见鸡是什么意思?做梦梦见鸡好不好 新鲜海带保存方法深海新鲜海带如何保存 学委怎么算综测 图腾领域第三季何时播出2022 图腾领域第三季青龙归来播出时间 白灼是不是就是水煮不放佐料? get 5 character tokens 一份执着的坚持语句通顺吗 如果定义不是那么的执着,这样的语句通顺吗? 用‘心’字组词 不能相同 使句子通顺 用下面词语中的五个写成一段话,要求语句通顺! 谁帮我看看语句通不通顺 (TGI)? on? m 《莫格利男孩》剧情乱成一锅粥,可观众却说:他把大家骗得好惨,为什么? 南山东海多少岁 北京飞纳泰科信息技术有限公司电话是多少?