「java写入xlsx」java写入文件时如何设置编码

博主:adminadmin 2023-03-21 17:04:10 555

本篇文章给大家谈谈java写入xlsx,以及java写入文件时如何设置编码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎么用java将一个excel里面数据读出并写入另一个excel?

需要对Excel中的数据进行读取操作。

一、在开始进行Java读写Excel前,需要先下一个jxl的jar包,这个jar包中提供了相关读写Excel的方法,将jxl.jar放到classpath下或者在工程的buildpath中添加jxl.jar后,便可以开始Java读写Excel了。

二、Java读取Excel数据,首先,创建一个xls文件(如:jxltest.xls),然后在文件中添加一些数据,Excel文件创建完成后,便可以开始写代码读取了。

三、进行一个小小的扩展,读取一个目录下的所有Excel文件,读取的每个Excel文件的数据写入到不同的txt中。

四、生成EXCEL需要手动写查询语句把ORACLE数据库中的数据查询出来,再通过操作写到EXCEL文件里面。通过EXCEL把数据读取到ORACLE,同样需要去读取EXCEL工作薄里面的内容,再通过INSERT语句去插入数据库操作。

java怎么把xls格式的文件另存为xlsx文件,不能直接改后缀名?

一般操作Excel有专业的工具库来实现,操作时,会考虑同时兼容不同版本的excel,你这里将xls转为xlsx,就是版本之间转换,可以参考以下代码的转换方法,方法还是比较简单,直接将文件另存为就可以了:

import com.spire.xls.*;

public class ExcelConversion {

public static void main(String[] args) {

Workbook wb = new Workbook();

wb.loadFromFile("test.xls");

wb.saveToFile("toXLSX.xlsx");

}

}

这里代码编译环境为IntelliJ IDEA,jdk是1.8.0版本,使用Excel库free spire.xls.jar 3.9.1。

java 将页面内容写入excel文件中并可以将其下载到本地任意位置

java本身要生成excel文件必然是在后台做的,通过poi库生成excel文件并制作表格。

无法直接通过网页保存生成excel。

至于下载到本地任意位置,也是后台生成了excel文件发送到前台(浏览器),由用户选择要存在哪儿,不能直接存储(这是web沙箱限制,不允许网页直接访问本地硬盘,不然你想想,如果你打开一个网页,网页代码可以任意访问你的硬盘,你还敢开网页吗)。

要绕过沙箱限制必须装插件,也就是,你必须开发一个com或plugin插件,可以访问本地硬盘,但这需要用户手工安装(比如flash的插件,你之所以能用网页看flash是因为装了它的插件,但这是你手工装的,它不能绕过你直接给你装,它必须询问你行不行,你要手工点了OK,才能装)

如何用java把数据写入到excel

需要导入jxl.jar

搭建环境

将下载后的文件解包,得到jxl.jar,放入classpath,安装就完成了。

创建文件

拟生成一个名为“测试数据.xls”的Excel文件,其中第一个工作表被命名为“第一页”,大致效果如下:

代码(CreateXLS.java):

//生成Excel的类

import java.io.*;

import jxl.*;

import jxl.write.*;

public class CreateXLS

{

public static void main(String args[])

{

try

{

//打开文件

WritableWorkbook book=

Workbook.createWorkbook(new File(“测试.xls”));

//生成名为“第一页”的工作表,参数0表示这是第一页

WritableSheet sheet=book.createSheet(“第一页”,0);

//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)

//以及单元格内容为test

Label label=new Label(0,0,”test”);

//将定义好的单元格添加到工作表中

sheet.addCell(label);

/*生成一个保存数字的单元格

必须使用Number的完整包路径,否则有语法歧义

单元格位置是第二列,第一行,值为789.123*/

jxl.write.Number number = new jxl.write.Number(1,0,789.123);

sheet.addCell(number);

//写入数据并关闭文件

book.write();

book.close();

}catch(Exception e)

{

System.out.println(e);

}

}

}

编译执行后,会在当前位置产生一个Excel文件。

JAVA怎么向Excel中写入批量数据?

public static void drawExcel(HSSFWorkbook wb, String sheetName, String title, int n, List exlList, int[] index){\x0d\x0aList exList =(List)exlList;\x0d\x0aint len = exList.get(0).length;\x0d\x0a// 创建一个sheet表单\x0d\x0aHSSFSheet sheet = wb.createSheet(sheetName);\x0d\x0aRegion region = null;\x0d\x0a//样式\x0d\x0aHSSFCellStyle cellStyle1 = setStyleBorder(wb);\x0d\x0aHSSFCellStyle cellStyle2 = setStyleFontBorder(wb);\x0d\x0a// 创建标题行\x0d\x0aHSSFRow row = sheet.createRow(0);\x0d\x0arow.setHeight((short)500);\x0d\x0a// 创建单元格\x0d\x0aHSSFCell cell = null;\x0d\x0aif(title != null !"".equals(title)){\x0d\x0aregion = new Region(0, (short)0, 0, (short)(len- 1));\x0d\x0asheet.addMergedRegion(region);\x0d\x0a\x0d\x0acell = row.createCell(0);\x0d\x0a// 标题写入单元格\x0d\x0acell.setCellValue(title);\x0d\x0a cell.setCellStyle(setStyleFontSize(wb, 18));\x0d\x0a}else{\x0d\x0an = n - 1;\x0d\x0a}\x0d\x0a\x0d\x0a NumberFormat formatter = NumberFormat.getNumberInstance();\x0d\x0a formatter.setMaximumFractionDigits(8);\x0d\x0a//合计信息\x0d\x0aDouble[] sum = new Double[len];\x0d\x0aObject[] s = null;\x0d\x0afor(int i = 0; i s = exList.get(i);\x0d\x0arow = sheet.createRow(i + n);\x0d\x0a// 创建数据行\x0d\x0afor(int j = 0; j cell = row.createCell(j);\x0d\x0a/***\x0d\x0a* jobin create\x0d\x0a*/\x0d\x0aif(s[j] instanceof Integer || s[j] instanceof Float){\x0d\x0acell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);\x0d\x0a}else{\x0d\x0acell.setCellType(HSSFCell.CELL_TYPE_STRING);\x0d\x0a}\x0d\x0aif(!StringUtils.isNotEmpty(s[j])){\x0d\x0acell.setCellStyle(cellStyle1);\x0d\x0acontinue;\x0d\x0a}\x0d\x0aString[] rs = null;\x0d\x0aint l = 3000;\x0d\x0aif(i == 0 s[j].toString().indexOf(",") 0){\x0d\x0ars = s[j].toString().split(",");\x0d\x0acell.setCellValue(rs[0]);\x0d\x0al = Integer.parseInt(rs[1]);\x0d\x0a}else{\x0d\x0acell.setCellValue(s[j].toString());\x0d\x0a}\x0d\x0aif(i == 0){\x0d\x0acell.setCellStyle(cellStyle2);\x0d\x0a//设置列宽\x0d\x0a//if(j == 0){\x0d\x0a sheet.setColumnWidth(j, l);\x0d\x0a//}else if(j == s.length -1){\x0d\x0a// sheet.setColumnWidth(j, 5000);\x0d\x0a//}else{\x0d\x0a// sheet.setColumnWidth(j, 3000);\x0d\x0a//}\x0d\x0a}else{\x0d\x0acell.setCellStyle(cellStyle1);\x0d\x0a//合计统计\x0d\x0aif(index != null){\x0d\x0afor(int in : index){\x0d\x0aif(in == j){\x0d\x0aif(sum[in] == null) sum[in] = 0.0;\x0d\x0asum[in] += Double.parseDouble(s[j].toString());\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a//合计信息\x0d\x0aif(index != null){\x0d\x0aregion = new Region(exList.size() + 1, (short)0, exList.size() + 1, (short)(index[0]-1));\x0d\x0asheet.addMergedRegion(region);\x0d\x0arow = sheet.createRow(exList.size() + n);\x0d\x0afor(int i = 0; i cell = row.createCell(i);\x0d\x0acell.setCellStyle(cellStyle2);\x0d\x0aif(i == 0){\x0d\x0acell.setCellValue("合计");\x0d\x0a}else if(sum[i] != null){\x0d\x0acell.setCellValue(formatter.format(sum[i]).replace(",", ""));\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a核心代码,我项目上使用的

JAVA编程中用Apache POI 怎么用SXSSFWorkbook对已存在的excel(.xlsx)操作进行写数据操作

XSSFWorkbook wb=new XSSFWorkbook(参数);中的参数是InputStream ,你直接XSSFWorkbook wb=new XSSFWorkbook(fs);就可以了。

第一步查询数据--这一步读者自行实现自己的数据查询 ListPointInfo points = null;

points = this.dao.getAllCollect(userId);

final MapString, ListPointInfo pointMap = new HashMap();

for (final PointInfo pointInfo : points) {

final String pt = pointInfo.getPointType(); if (pointMap.containsKey(pt)) {final ListPointInfo subList = pointMap.get(pt);

subList.add(pointInfo);

} else {final ListPointInfo subList = new ArrayList();subList.add(pointInfo);

pointMap.put(pt, subList

第二步:生成工作簿

final SXSSFWorkbook wb = new SXSSFWorkbook();

// 对每一种类型生成一个sheet

for (final Map.EntryString, ListPointInfo entry : pointMap.entrySet()) {

final ListPointInfo pts = entry.getValue();

// 获取每种类型的名字--作为sheet显示名称--如果不需要分sheet可忽略

String typeName = "";

if (this.dao.getTypeByTypeCode(pts.get(0).getPointType()) != null) {

typeName = this.dao.getTypeByTypeCode(pts.get(0).getPointType()).getPointTypeName();

}

final Sheet sheet = wb.createSheet(typeName);

//生成用于插入图片的容器--这个方法返回的类型在老api中不同

final Drawing patriarch = sheet.createDrawingPatriarch();

// 为sheet1生成第一行,用于放表头信息

final Row row = sheet.createRow(0);

// 第一行的第一个单元格的值

Cell cell = row.createCell((short) 0);

cell.setCellValue("详细地址");

cell = row.createCell((short) 1);

cell.setCellValue("经度");

cell = row.createCell((short) 2);

cell.setCellValue("纬度");

cell = row.createCell((short) 3);

for (int i = 0; i pts.size(); i++) {

final Row each = sheet.createRow(i + 1);

Cell infoCell = each.createCell((short) 0);

infoCell.setCellValue(pts.get(i).getAddrDetail());

infoCell = each.createCell((short) 1);

infoCell.setCellValue(pts.get(i).getX());

infoCell = each.createCell((short) 2);

infoCell.setCellValue(pts.get(i).getY());

infoCell = each.createCell((short) 3);

//查询获取图片路径信息--该步读者自定义

PointPic pic = this.dao.getPicInfoByPointId(pts.get(i).getId());

try {

if (pic != null) {

for (int k = 0; k 6; k++) {//因为有六张图片,所以循环6次

final short colNum = (short) (4+k);

infoCell = each.createCell(colNum);

BufferedImage img = null;

switch (k) {

case 0:

if (!StringUtils.isEmpty(pic.getPicOneAddr())) {

File imgFile = new File(pic.getPicOneAddr());

img = ImageIO.read(imgFile);

imgFile = null;

}

break;

case 1:

if (!StringUtils.isEmpty(pic.getPicTwoAddr())) {

File imgFile = new File(pic.getPicTwoAddr());

img = ImageIO.read(imgFile);

imgFile = null;

}

break;

case 2:

if (!StringUtils.isEmpty(pic.getPicThreeAddr())) {

File imgFile = new File(pic.getPicThreeAddr());

img = ImageIO.read(imgFile);

imgFile = null;

}

break;

case 3:

if (!StringUtils.isEmpty(pic.getPicFourAddr())) {

File imgFile = new File(pic.getPicFourAddr());

img = ImageIO.read(imgFile);

imgFile = null;

}

break;

case 4:

if (!StringUtils.isEmpty(pic.getPicFiveAddr())) {

File imgFile = new File(pic.getPicFiveAddr());

img = ImageIO.read(imgFile);

imgFile = null;

}

break;

case 5:

if (!StringUtils.isEmpty(pic.getPicSixAddr())) {

File imgFile = new File(pic.getPicSixAddr());

img = ImageIO.read(imgFile);

imgFile = null;

}

break;

}

ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

ImageIO.write(img, "jpg", byteArrayOut);

img = null;

//设置每张图片插入位置

final XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, colNum,

i + 1, (short) (colNum + 1), i + 2);//参数为图片插入在表格的坐标,可以自行查看api研究参数

anchor.setAnchorType(0);

// 插入图片

patriarch.createPicture(anchor, wb.addPicture(

byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

byteArrayOut.close();

byteArrayOut = null;

}

pic = null;

}

} catch (final Exception e) {

e.printStackTrace();

}

}

}

final ByteArrayOutputStream os = new ByteArrayOutputStream();

try {

wb.write(os);

} catch (final IOException e) {

e.printStackTrace();

}

final byte[] content = os.toByteArray();

final String url = Var.BASE_URL+ File.separator + "output.xls";//读者自定义路径

final File file = new File(url);// Excel文件生成后存储的位置。

OutputStream fos = null;

try {

fos = new FileOutputStream(file);

fos.write(content);

os.close();

fos.close();

} catch (final Exception e) {

e.printStackTrace();

}

return url;//文件保存成功

java写入xlsx的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java写入文件时如何设置编码、java写入xlsx的信息别忘了在本站进行查找喔。