三九宝宝网宝宝百科宝宝知识

java端导出Excel表格

03月19日 编辑 39baobao.com

[怎么用java实现读取excel表格里的数据生成]public class AnalyzeExcel { public static void main(String args[]) throws FileNotFoundException, IOException { XSSFWorkbook wb = new XSSFWorkbook("D:/TA/Weeky.xl...+阅读

可以使用poi来实现导出execl表格或者通过io流实现导出execl表格,但是poi相对来说更方便 实例如下: try{ HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+3);//创建所需的行数 for(int j=0; j HSSFCell cell = null; //设置单元格的数据类型 if(j == 0){ cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) & obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } if(workbook !=null){ try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response = getResponse(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); OutputStream out = response.getOutputStream(); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } }

java怎么导出excel表格

通过这个例子,演示以下如何用java生成excel文件:

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 POI生成Excel表文件

private static void WriteExcel2010() throws IOException {

String path="C:\\poi2.xlsx";

XSSFWorkbook workbook=new XSSFWorkbook();

XSSFSheet sheet=workbook.createSheet("我的Sheet");

XSSFRow row=sheet.createRow(0);

XSSFCell cell=row.createCell(0);

cell.setCellValue("我是POI写入的");

XSSFRow row1=sheet.createRow(1);

XSSFCell cell1=row1.createCell(0);

cell1.setCellValue("2010");

FileOutputStream outputStream=new FileOutputStream(path);

workbook.write(outputStream);

outputStream.close();

}

private static void WriteExcel2003() throws IOException {

String path="C:\\poi2.xls";

HSSFWorkbook workbook=new HSSFWorkbook();

HSSFSheet sheet=workbook.createSheet("我的Excel");

HSSFRow row=sheet.createRow(0);

HSSFCell cell=row.createCell(0);

cell.setCellValue("我是POI写入的");

FileOutputStream outputStream=new FileOutputStream(path);

workbook.write(outputStream);

outputStream.close();

}

以下为关联文档:

怎么用java导出word文档java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台...

把金蝶K3精益版的数据导出为Excel表格格式 Excel表格的数据导入我们初始化的时候是从这个地方导入的,不知道你问的是不是这个问题 数据库资料修改我们没有操作过,你可以试试。查文件格式的扩展名为SQL的。 这个财务软件公司已经没在用了,也...

如何导入和导出表格数据方法/步骤 1 比如下图的word文档,里面的数据只是用中文的逗号分隔了。首先将这些复制粘贴到excel,我们发现这些数据都在一列里面,这不是我们想要的。 2 我们应该选中这个列,在数...

Java利用poi导出excel表格如何在导出时自由选择路径导出时自由选择路径的代码如下: 1、后台输出Excel文件代码:OutputStream output = response.getOutputStream();response.reset();response.setHeader("Content-disposition", "a...

java大概1000W数据导出成excel有什么好的建议使用POI或JXL的话,数据是一次性读入内存,封装成对象,再一次性导出的。这么大的数据量都有G级了吧,如果是一次性作业的话,内存很难受。 考虑分批,追加写入的方式,对于简单的字节字符...

php导出生成excel表格几种方法介绍php header("Content-type:application/vnd.ms-excel");header("Content-Disposition:attachment;filename=test_data.xls");$tx='表头';echo $tx."nn";//输出内容如下:echo "姓名"."t";e...

PHP如何把数据库导出EXCEL表格< ?php function getmicrotime(){ list($usec, $sec) = explode(” “,microtime()); return ((float)$usec + (float)$sec); } ?> < ?php $time_start = getmicrotime(); i...

EXCEL表格如何导出至WORD格式使用插入[对象]的方法比较简单,而且可以即时更改编辑excel 方法:单击 [ 插入]---[ 文本 ]---[ 对象 ] 出现[对话框] ---选择[ 由文件创建 ] --- 选择相关excel --确定插入 方...

怎么用java实现读取excel表格里的数据生成曲线图首先使用JXL读取excel的数据 然后使用JFreeChart把数据转成曲线图 说明: jxl.jar是通过java操作excel表格的工具类库支持Excel 95-2000的所有版本 JFreeChart是JAVA平台上的...

推荐阅读
图文推荐