java写入excel
⑴ java 将页面内容写入excel文件中并可以将其下载到本地任意位置
java本身要生成excel文件必然是在后台做的,通过poi库生成excel文件并制作表格。
无法直接通过网页保存生成excel。
至于下载到本地任意位置,也是后台生成了excel文件发送到前台(浏览器),由用户选择要存在哪儿,不能直接存储(这是web沙箱限制,不允许网页直接访问本地硬盘,不然你想想,如果你打开一个网页,网页代码可以任意访问你的硬盘,你还敢开网页吗)。
要绕过沙箱限制必须装插件,也就是,你必须开发一个com或plugin插件,可以访问本地硬盘,但这需要用户手工安装(比如flash的插件,你之所以能用网页看flash是因为装了它的插件,但这是你手工装的,它不能绕过你直接给你装,它必须询问你行不行,你要手工点了OK,才能装)
⑵ 怎样用java向excel中写数据
public void export(List<PSbLnode> li, String dateString,String[] title) throws WriteException, IOException {
// 准备设置excel工作表的标题
// 创建Excel工作薄
WritableWorkbook wwb = null;
try {
// 输出的excel的路径
String filePath1 = Const.pathStr+Const.pathStr4+Const.pathStr3;
File file = new File(filePath1);
if(!file.exists()){
file.mkdir();
}
String filePath=filePath1+Const.pathStr4+Const.pathStr6+dateString+Const.pathStr5;
// 新建立一个jxl文件,即在C盘下生成testJXL.xls
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheet = wwb.createSheet("设备清单", 0);
Label label;
for (int i = 0; i < title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
label = new Label(i, 0, title[i]);
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
}
for (int i = 0; i < li.size(); i++) {
int j = 0;
j = i + 1;
//填充单元格
//获取区域名称
label = new Label(0, j, li.get(i).getQyName());
sheet.addCell(label);
//获取区域名称
label = new Label(1, j, li.get(i).getJzName());
sheet.addCell(label);
//获取设备名称
label = new Label(2, j, li.get(i).getLnodeName());
sheet.addCell(label);
// //获取设备类型名称
label = new Label(3, j, li.get(i).getSbxh());
sheet.addCell(label);
//获取运行状态
label = new Label(4, j, li.get(i).getYxzh());
sheet.addCell(label);
//获取删除状态
label = new Label(5, j, li.get(i).getDeleteFlag());
sheet.addCell(label);
//获取启用状态
label = new Label(6, j, li.get(i).getQyzt());
sheet.addCell(label);
//获取设备投运日期
label = new Label(7, j, li.get(i).getSbtyri());
sheet.addCell(label);
//获取使用年限
jxl.write.Number numb1 = new jxl.write.Number(8, j, li.get(i).getSynx());
sheet.addCell(numb1);
//获取区域名称
label = new Label(9, j, li.get(i).getAddUser());
sheet.addCell(label);
//获取区域名称
label = new Label(10, j, li.get(i).getUpdUser());
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String newdate = sdf.format(li.get(i).getUpdTime());
label = new Label(11, j, newdate);
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String newdate2 = sdf2.format(li.get(i).getAddTime());
label = new Label(12, j, newdate2);
sheet.addCell(label);
//获取区域名称
label = new Label(13, j, li.get(i).getZcbh());
sheet.addCell(label);
//获取区域名称
label = new Label(14, j, li.get(i).getSbcs());
sheet.addCell(label);
//获取区域名称
jxl.write.Number numb2 = new jxl.write.Number(15, j, li.get(i)
.getSbll());
sheet.addCell(numb2);
//获取区域名称
label = new Label(16, j, li.get(i).getRldw());
sheet.addCell(label);
//获取区域名称
label = new Label(17, j, li.get(i).getWxghjl());
sheet.addCell(label);
}
// 写入数据
wwb.write();
} catch (Exception e) {
e.printStackTrace();
}finally{
// 关闭文件
wwb.close();
}
}
Const文件:
/**路径:C盘*/
public static String pathStr = "C:";
/**路径://*/
public static String pathStr2 = "//";
/**文件夹:workspace*/
public static String pathStr3 = "exportFile";
/**文件名:world*/
public static String pathStr6 = "Equipment";
/**路径:/*/
public static String pathStr4 = "/";
/**路径:.xls*/
public static String pathStr5 = ".xls";
⑶ 浅谈JAVA读写Excel的几种途径
读写Excel文件需要使用Excel类库,如Free Spire.XLS for Java.
读取Excel内容:
//创建Workbook对象
Workbookwb=newWorkbook();
//加载一个Excel文档
wb.loadFromFile("C:\Users\Administrator\Desktop\test.xlsx");
//获取第一个工作表
Worksheetsheet=wb.getWorksheets().get(0);
//遍历工作表的每一行
for(inti=1;i<sheet.getLastRow()+1;i++){
//遍历工作的每一列
for(intj=1;j<sheet.getLastColumn()+1;j++){
//输出指定单元格的数据
System.out.print(sheet.get(i,j).getText());
System.out.print(" ");
}
System.out.print(" ");
}
写入内容:
//创建Workbook对象
Workbookwb=newWorkbook();
//加载一个Excel文档
wb.loadFromFile("C:\Users\Administrator\Desktop\test.xlsx");
//获取第一个工作表
Worksheetsheet=wb.getWorksheets().get(0);
//在单元格A1写入新数据
sheet.getCellRange("A1").setText("你好");
//保存文档
wb.saveToFile("写入Excel.xlsx",ExcelVersion.Version2016);
⑷ 如何用java向指定的Excel中写入数据
jxl,可以实现,开源的jar包很多,目前我使用的是jxl
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String id =req.getParameter("id");
String fileName =req.getParameter("fileName");
File f = new File(req.getSession().getServletContext().getRealPath("/printTemplateExcel/"+fileName));
WritableWorkbook wwb=null;
WritableSheet wws=null;
FileOutputStream out =null;
Label label = null;
Workbook wb=null;
try {
FesOrders fesOrders =new FesOrders();
fesOrders.setId(Long.parseLong(id));
List<FesOrders> fesOrdersList = fesOrderService.findList(fesOrders, (SysUser)req.getSession().getAttribute(SESSION_USER));
for(FesOrders a : fesOrdersList){
System.out.println(a.getId());
}
WritableFont headerFont =new WritableFont(WritableFont.createFont("新宋体"), 10, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat dataFormat=new WritableCellFormat (headerFont);
dataFormat.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//设置边框
WritableCellFormat wf = new WritableCellFormat(dataFormat);
wb = Workbook.getWorkbook(f);
out = new FileOutputStream(new File(req.getSession().getServletContext().getRealPath("/printExcel/"+fileName)));
wwb = Workbook.createWorkbook(out, wb);
wws = wwb.getSheet("出货单");
String date=sdf.format(new Date());
for(int i=0;i<wws.getRows();i++){
for(int j=0;j<wws.getColumns();j++){
Cell cell = wws.getCell(j, i);
if(cell.getContents().trim().indexOf("${year}")>0){
label = (Label)cell;
label.setString(cell.getContents().replace("${year}", date.subSequence(0, 4)));
}
if(cell.getContents().trim().indexOf("${mouth}")>0){
label = (Label)cell;
label.setString(cell.getContents().replace("${mouth}", date.substring(5, 7)));
}
if(cell.getContents().trim().indexOf("${day}")>0){
label = (Label)cell;
label.setString(cell.getContents().replace("${day}", date.substring(8, 10)));
}
}
}
System.out.println(fesOrdersList.size());
for(int i=0;i<fesOrdersList.size();i++){
FesOrders fesOrder= fesOrdersList.get(i);
List<FesSendOrders> fesSendOrders=fesOrder.getFesSendOrderses();
wws.addCell(new Label(0,i+2,i+1+"",wf));
wws.addCell(new Label(1,i+2,fesOrder.getSysUser().getCnName(),wf));
wws.addCell(new Label(2,i+2,fesOrder.getInnerSendNo(),wf));
wws.addCell(new Label(3,i+2,fesSendOrders.get(0).getFesTransportModes().getName(),wf));
if(fesSendOrders.get(0).getOpWeight()!=null){
wws.addCell(new Label(4,i+2,fesSendOrders.get(0).getOpWeight().toString(),wf));
}
wws.addCell(new Label(5,i+2,"",wf));
wws.addCell(new Label(6,i+2,fesSendOrders.get(0).getFesAddresseesBySenderId().getAddress(),wf));
wws.addCell(new Label(7,i+2,"",wf));
wws.addCell(new Label(8,i+2,fesOrder.getOrderNo(),wf));
wws.addCell(new Label(9,i+2,"",wf));
}
wwb.write();
wwb.close();
out.flush();
out.close();
download(req,res,fileName);
这个是我写的简单的导出excel
⑸ java 如何将字符串写入一个已有的excel表格中
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class SaveExcel {
public void writeExcel(String path) {
WritableWorkbook wb;
try {
wb = Workbook.createWorkbook(new File(path));
WritableSheet ws = wb.createSheet("sheet1", 0);
for(int i=0;i<5;i++){
Number labelN = new Number(1, i, i);
ws.addCell(labelN);
}
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new SaveExcel().writeExcel("D:\\111\\R1.xls");
}
}
你要去下一个jxl.jar
⑹ Java 向Excel指定单元格写入数据
Labelxuexiao=newLabel(1,1,"aaaaa");
sheet.addCell(xuexiao);//addCell
book.write();//write下有红线
参数,或参考类型都不正确。。。。。。。。。。。
WritableCell
WritableSheet
等
⑺ 怎么用java把数据库里的数据写入到excel表中
java 读excel 还是比较方便简单的,原理就是,先用java 读取excel,然后,一行行的写入数据库,字段的话,你自己程序里面写就行了,给你个例子:
从Excel读取数据,生成新的Excel,以及修改Excel
package common.util;
import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.Boolean;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: xl
* Date: 2005-7-17
* Time: 9:33:22
* To change this template use File | Settings | File Templates.
*/
public class ExcelHandle
{
public ExcelHandle()
{
}
/**
* 读取Excel
*
* @param filePath
*/
public static void readExcel(String filePath)
{
try
{
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
//Sheet st = rwb.getSheet("0")这里有两种方法获取sheet表,1为名字,而为下标,从0开始
Sheet st = rwb.getSheet("original");
Cell c00 = st.getCell(0,0);
//通用的获取cell值的方式,返回字符串
String strc00 = c00.getContents();
//获得cell具体类型值的方式
if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00.getString();
}
//输出
System.out.println(strc00);
//关闭
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 输出Excel
*
* @param os
*/
public static void writeExcel(OutputStream os)
{
try
{
/**
* 只能通过API提供的工厂方法来创建Workbook,而不能使用WritableWorkbook的构造函数,
* 因为类WritableWorkbook的构造函数为protected类型
* method(1)直接从目标文件中读取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));
* method(2)如下实例所示 将WritableWorkbook直接写入到输出流
*/
WritableWorkbook wwb = Workbook.createWorkbook(os);
//创建Excel工作表 指定名称和位置
WritableSheet ws = wwb.createSheet("Test Sheet 1",0);
//**************往工作表中添加数据*****************
//1.添加Label对象
Label label = new Label(0,0,"this is a label test");
ws.addCell(label);
//添加带有字型Formatting对象
WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label labelcf = new Label(1,0,"this is a label test",wcf);
ws.addCell(labelcf);
//添加带有字体颜色的Formatting对象
WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
ws.addCell(labelCF);
//2.添加Number对象
Number labelN = new Number(0,1,3.1415926);
ws.addCell(labelN);
//添加带有formatting的Number对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
ws.addCell(labelNF);
//3.添加Boolean对象
Boolean labelB = new jxl.write.Boolean(0,2,false);
ws.addCell(labelB);
//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
ws.addCell(labelDT);
//添加带有formatting的DateFormat对象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
ws.addCell(labelDTF);
//添加图片对象,jxl只支持png格式图片
File image = new File("f:\\2.png");
WritableImage wimage = new WritableImage(0,1,2,2,image);
ws.addImage(wimage);
//写入工作表
wwb.write();
wwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 拷贝后,进行修改,其中file1为被对象,file2为修改后创建的对象
* 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,
* 以使单元格的内容以不同的形式表现
* @param file1
* @param file2
*/
public static void modifyExcel(File file1,File file2)
{
try
{
Workbook rwb = Workbook.getWorkbook(file1);
WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//
WritableSheet ws = wwb.getSheet(0);
WritableCell wc = ws.getWritableCell(0,0);
//判断单元格的类型,做出相应的转换
if(wc.getType == CellType.LABEL)
{
Label label = (Label)wc;
label.setString("The value has been modified");
}
wwb.write();
wwb.close();
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
⑻ 怎么用java将一个excel里面数据读出并写入另一个excel
需要对Excel中的数据进行读取操作。