兄弟,那个用java导入excel表,弹出下载框的代码能否发一份给我借鉴借鉴?
2个回答
展开全部
public void creatExcel(JFileChooser file, DefaultTableModel defaultTableModel, JTable defectsTable){
jxl.write.Label labelS = null;
Number labelN = null;
try{
file.showSaveDialog(null); //保存dialog
fout = file.getSelectedFile(); //Excel文件名字后缀是.xls
if (fout==null){
}
else {
fout=new File(fout.getAbsolutePath()+".xls"); //文件名绝对路劲自动以.xls结尾
wwb = Workbook.createWorkbook(fout);
ws = wwb.createSheet("QFC控制器数据分析表",0); //Excel表格的表单
ws.getSettings().setVerticalFreeze(3);
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,10,
WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf); //NumberFormats.TEXT
jxl.write.Label labelCF = new jxl.write.Label(12,1,"制表时间",wcfF);
ws.addCell(labelCF);
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,20,
WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);
wchB.setAlignment(jxl.format.Alignment.CENTRE);
jxl.write.Label labelCFC = new jxl.write.Label(9,0,sortedList.getSelectedValue()+"-"+
"QFC控制器整点数据报表",wchB);
ws.addCell(labelCFC);
ws.mergeCells(13,1,14,1);// (水平左,垂直上,水平右,垂直下)
jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd HH:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(13,1,new java.util.Date(),wcfDF);
ws.addCell(labelDTF);
for(int i = 0;i<defaultTableModel.getColumnCount();i++){
jxl.write.Label label = new jxl.write.Label(i,2,defaultTableModel.getColumnName(i));
ws.addCell(label);
}
for(int j =0;j<defectsTable.getRowCount();j++){
for(int i = 0;i<defectsTable.getColumnCount();i++){
//以文本形式导入保证数据不会丢失
WritableCellFormat contentFromart = new WritableCellFormat(NumberFormats.TEXT);
if(defectsTable.getValueAt(j, i).getClass().equals(Integer.class)){
labelN = new Number(i,j+3,(Integer)defectsTable.getValueAt(j, i),contentFromart);
ws.addCell(labelN);
}else if(defectsTable.getValueAt(j, i).getClass().equals(String.class)){
labelS = new jxl.write.Label(i,j+3,(String)defectsTable.getValueAt(j, i),contentFromart);
ws.addCell(labelS);
}
}
}
//将内容写到文件中
wwb.write();
//将wwb关闭
wwb.close();
JOptionPane.showMessageDialog(null,"导出整点数据到"+fout.getName()+"成功","成功",
JOptionPane.INFORMATION_MESSAGE); }
}catch(FileNotFoundException fnfe){
JOptionPane.showMessageDialog(this, "文件没有找到");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.print("输入输出异常");
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
System.out.print("Excel导入异常");
} catch (WriteException e) {
// TODO Auto-generated catch block
System.out.print("写入Excel异常");
}
}
jxl.write.Label labelS = null;
Number labelN = null;
try{
file.showSaveDialog(null); //保存dialog
fout = file.getSelectedFile(); //Excel文件名字后缀是.xls
if (fout==null){
}
else {
fout=new File(fout.getAbsolutePath()+".xls"); //文件名绝对路劲自动以.xls结尾
wwb = Workbook.createWorkbook(fout);
ws = wwb.createSheet("QFC控制器数据分析表",0); //Excel表格的表单
ws.getSettings().setVerticalFreeze(3);
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,10,
WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf); //NumberFormats.TEXT
jxl.write.Label labelCF = new jxl.write.Label(12,1,"制表时间",wcfF);
ws.addCell(labelCF);
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,20,
WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);
wchB.setAlignment(jxl.format.Alignment.CENTRE);
jxl.write.Label labelCFC = new jxl.write.Label(9,0,sortedList.getSelectedValue()+"-"+
"QFC控制器整点数据报表",wchB);
ws.addCell(labelCFC);
ws.mergeCells(13,1,14,1);// (水平左,垂直上,水平右,垂直下)
jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd HH:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(13,1,new java.util.Date(),wcfDF);
ws.addCell(labelDTF);
for(int i = 0;i<defaultTableModel.getColumnCount();i++){
jxl.write.Label label = new jxl.write.Label(i,2,defaultTableModel.getColumnName(i));
ws.addCell(label);
}
for(int j =0;j<defectsTable.getRowCount();j++){
for(int i = 0;i<defectsTable.getColumnCount();i++){
//以文本形式导入保证数据不会丢失
WritableCellFormat contentFromart = new WritableCellFormat(NumberFormats.TEXT);
if(defectsTable.getValueAt(j, i).getClass().equals(Integer.class)){
labelN = new Number(i,j+3,(Integer)defectsTable.getValueAt(j, i),contentFromart);
ws.addCell(labelN);
}else if(defectsTable.getValueAt(j, i).getClass().equals(String.class)){
labelS = new jxl.write.Label(i,j+3,(String)defectsTable.getValueAt(j, i),contentFromart);
ws.addCell(labelS);
}
}
}
//将内容写到文件中
wwb.write();
//将wwb关闭
wwb.close();
JOptionPane.showMessageDialog(null,"导出整点数据到"+fout.getName()+"成功","成功",
JOptionPane.INFORMATION_MESSAGE); }
}catch(FileNotFoundException fnfe){
JOptionPane.showMessageDialog(this, "文件没有找到");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.print("输入输出异常");
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
System.out.print("Excel导入异常");
} catch (WriteException e) {
// TODO Auto-generated catch block
System.out.print("写入Excel异常");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询