
正文
java导出报表代码 java导出xlsx
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
跪求JAVA Eclipse 导出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文件。
相关问答
Q1: java端导出Excel表格。
可以使用poi来实现导出execl表格或者通过io流实现导出execl表格java导出报表代码,但是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); // 在索引2java导出报表代码的位置创建行(最顶端的行开始的第二行)
// 将列头设置到sheet的单元格中
for(int n=0;ncolumnNum;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;idataList.size();i++){
Object[] obj = dataList.get(i);//遍历每个对象
HSSFRow row = sheet.createRow(i+3);//创建所需的行数
for(int j=0; jobj.length; 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 columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum sheet.getLastRowNum(); 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 length) {
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();
}
}
Q2: java中怎么把报表导出到excel
导入数据库
@RequestMapping("/uploadOrderFile")
@ResponseBody
public Object uploadOrderFile(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "file") MultipartFile[] files) throws ServletException, IOException, ParseException{
Workbook rwb=null;
if(files!=null files.length0){
try {
// String filePath = request.getSession().getServletContext().getRealPath("/") + "\\uploadOrderFile\\" + files.getOriginalFilename();
// System.out.println("----------"+filePath);
rwb = Workbook.getWorkbook(files[0].getInputStream());
Sheet rs=rwb.getSheet(0);//默认0是第一张表java导出报表代码,或者rwb.getSheet(Sheet1)Excel要导入的表名
int clos=rs.getColumns();//得到所有的列
int rows=rs.getRows();//得到所有的行
//存放Excel表抬头名称以及对应的列
MapInteger,Object map=new HashMapInteger, Object();
//实体类集合存放每次循环获得的值
ListMedicine medicineList=new ArrayListMedicine();
for (int i = 0; i rows; i++) {
//创建实体类
Medicine medicine=new Medicine();
if(i==0){//遍历第一行获取抬头跟对应的列
for (int j = 0; j clos; j++) {//取得每个抬头名称对应的列
if(rs.getCell(j, i).getContents().equals("品名")){
map.put(j,"品名");
}else if(rs.getCell(j, i).getContents().equals("商品编号")){
map.put(j,"商品编号");
}else if(rs.getCell(j, i).getContents().equals("生产日期")){
map.put(j,"生产日期");
}else if(rs.getCell(j, i).getContents().equals("产地")){
map.put(j,"产地");
}else if(rs.getCell(j, i).getContents().equals("生产厂家")){
map.put(j,"生产厂家");
}else if(rs.getCell(j, i).getContents().equals("批号")){
map.put(j,"批号");
}
}
}else{
//循环遍历map 》》》存的Excel表的抬头名称以及对应的列
for (int j = 0; j clos; j++) {
if(map.get(j)==null){//如果=null 进入下一个循环
continue;
}
if(map.get(j).equals("品名")){
//如果为空结束当前循环java导出报表代码,进入下一个循环
if(rs.getCell(j, i).getContents()==null||rs.getCell(j, i).getContents().equals("")){
continue;
}
medicine.setMedicineName(rs.getCell(j, i).getContents());
}else if(map.get(j).equals("商品编号")map.get(j)!=null){
//如果为空结束当前循环,进入下一个循环
if(rs.getCell(j, i).getContents()==null||rs.getCell(j, i).getContents().equals("")){
continue;
}
medicine.setMedicineCode(rs.getCell(j, i).getContents());
}else if(map.get(j).equals("生产日期")map.get(j)!=null){
medicine.setCreateTime(rs.getCell(j, i).getContents());
if(rs.getCell(j, i).getContents()!=null !rs.getCell(j, i).getContents().equals("")){
//如果生产日期存在 则+三年给到期日期赋值
//CommonUtil.getMedicineEffectiveTime为封装好的类
medicine.setEffectTime(CommonUtil.getMedicineEffectiveTime(rs.getCell(j, i).getContents(),3));
}
}else if(map.get(j).equals("产地")map.get(j)!=null){
medicine.setAddress(rs.getCell(j, i).getContents());
}else if(map.get(j).equals("生产厂家")map.get(j)!=null){
medicine.setProducingArea(rs.getCell(j, i).getContents());
}else if(map.get(j).equals("批号")map.get(j)!=null){
medicine.setBatchNumber(rs.getCell(j, i).getContents());
}
}
medicineList.add(medicine);//获得的值放入集合中
}
}
//新增用到的list
ListMedicine addList=new ArrayListMedicine();
//修改用到的list
ListMedicine updateList=new ArrayListMedicine();
//导入数据
for(int i=0;imedicineList.size();i++){
//判断商品编号是否存在
if(medicineService.selectMedicineCode(medicineList.get(i).getMedicineCode()).size()0){
//如果存在则修改
updateList.add(medicineList.get(i));
}else{
addList.add(medicineList.get(i));
}
}
int update=0;
int add=0;
if(updateList!=nullupdateList.size()0){
update=medicineService.updateMedicine(updateList);
}
if(addList!=nulladdList.size()0){
add= medicineService.addMedicine(addList);
}
if(update0||add0){
return new ResponseModel().attr(ResponseModel.KEY_DATA,"数据导入成功!");
}else{
return new ResponseModel().attr(ResponseModel.KEY_ERROR,"数据导入失败!");
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
return new ResponseModel().attr(ResponseModel.KEY_ERROR,"没有需要导入的数据!");
}
return null;
}
导出
package beans.excel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Boolean;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class MutiStyleExcelWrite {
public void createExcel(OutputStream os) throws WriteException,IOException {
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//构造表头
sheet.mergeCells(0, 0, 4, 0);//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
WritableFont bold = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat titleFormate = new WritableCellFormat(bold);//生成一个单元格样式控制对象
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中
Label title = new Label(0,0,"JExcelApi支持数据类型详细说明",titleFormate);
sheet.setRowView(0, 600, false);//设置第一行的高度
sheet.addCell(title);
//创建要显示的具体内容
WritableFont color = new WritableFont(WritableFont.ARIAL);//选择字体
color.setColour(Colour.GOLD);//设置字体颜色为金黄色
WritableCellFormat colorFormat = new WritableCellFormat(color);
Label formate = new Label(0,1,"数据格式",colorFormat);
sheet.addCell(formate);
Label floats = new Label(1,1,"浮点型");
sheet.addCell(floats);
Label integers = new Label(2,1,"整型");
sheet.addCell(integers);
Label booleans = new Label(3,1,"布尔型");
sheet.addCell(booleans);
Label dates = new Label(4,1,"日期格式");
sheet.addCell(dates);
Label example = new Label(0,2,"数据示例",colorFormat);
sheet.addCell(example);
//浮点数据
//设置下划线
WritableFont underline= new WritableFont(WritableFont.ARIAL,WritableFont.DEFAULT_POINT_SIZE,WritableFont.NO_BOLD,false,UnderlineStyle.SINGLE);
WritableCellFormat greyBackground = new WritableCellFormat(underline);
greyBackground.setBackground(Colour.GRAY_25);//设置背景颜色为灰色
Number number = new Number(1,2,3.1415926535,greyBackground);
sheet.addCell(number);
//整形数据
WritableFont boldNumber = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑体
WritableCellFormat boldNumberFormate = new WritableCellFormat(boldNumber);
Number ints = new Number(2,2,15042699,boldNumberFormate);
sheet.addCell(ints);
//布尔型数据
Boolean bools = new Boolean(3,2,true);
sheet.addCell(bools);
//日期型数据
//设置黑体和下划线
WritableFont boldDate = new WritableFont(WritableFont.ARIAL,WritableFont.DEFAULT_POINT_SIZE,WritableFont.BOLD,false,UnderlineStyle.SINGLE);
WritableCellFormat boldDateFormate = new WritableCellFormat(boldDate,DateFormats.FORMAT1);
Calendar c = Calendar.getInstance();
Date date = c.getTime();
DateTime dt = new DateTime(4,2,date,boldDateFormate);
sheet.addCell(dt);
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
}
java导出报表代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java导出xlsx、java导出报表代码的信息别忘了在本站进行查找喔。







