java 操作Excel 最近在做一个用java API操作Excel的程序,想把Excel的内容读出来后,进行更改,比如说我读了一个单元格的内容,我想直接把它更改了。但是这样好像不行呀!必须要创建一个 WritableWorkbook的类才可以进行更改,请问我不要WritableWorkbook类,直接把读出来的内容进行更改可以吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用 POI 吧,方便得多,不需要区分什么读写 Workbook 的。 楼主用的应该是jxl吧.这个东西我也用过,它所谓的修改,其实就是重新对单元格的内容进行写入,和你第一次写入单元格内容是一样的操作,所以必须用到WritableWorkbook类. 对的 jxl必须这样的jxl其实比PIO容易学 去下个jxl.jar可以生成Excel的 貌似现在poi更强大些打算尝试 下载一个poi 传递一个jtable 就直接打印了package com.fox_ice.TestDB;import java.awt.*;import javax.swing.*;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import javax.swing.table.TableModel;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.hssf.usermodel.HSSFFont;import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import java.io.FileOutputStream;public class Ipexcel extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); JTable jtable; public Ipexcel(JTable jtable) { try { this.jtable=jtable; setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Component initialization. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(borderLayout1); setSize(new Dimension(400, 300)); setTitle("Frame Title"); FileDialog fd=new FileDialog(this,"选择保存文件的地址", FileDialog.SAVE); fd.setFile("这里修改文件名.xls"); fd.show();String strFile=fd.getDirectory()+fd.getFile();/* JFileChooser chooser = new JFileChooser(".");File s=chooser.getCurrentDirectory() ;String s1=s+"";chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);JavaFileFilter javaFilter = new JavaFileFilter();chooser.addChoosableFileFilter(javaFilter);chooser.setFileFilter(javaFilter);int rs = chooser.showSaveDialog(Main.this); */FileOutputStream fos = new FileOutputStream(strFile);// JFileChooser jfc=new JFileChooser(); HSSFWorkbook wb=new HSSFWorkbook(); HSSFSheet hs=wb.createSheet(); TableModel tm=jtable.getModel(); int row=tm.getRowCount(); int cloumn=tm.getColumnCount(); HSSFCellStyle style=wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short)11); style.setFont(font); HSSFCellStyle style1=wb.createCellStyle(); style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); style1.setBorderLeft(HSSFCellStyle.BORDER_THIN); style1.setBorderRight(HSSFCellStyle.BORDER_THIN); style1.setBorderTop(HSSFCellStyle.BORDER_THIN); style1.setFillForegroundColor(HSSFColor.ORANGE.index); style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font1 = wb.createFont(); font1.setFontHeightInPoints((short)15); font1.setBoldweight((short)700); style1.setFont(font); for(int i=0;i<row+1;i++) { HSSFRow hr=hs.createRow(i); for(int j=0;j<cloumn;j++) { if(i==0) { String value=tm.getColumnName(j); int len=value.length(); hs.setColumnWidth((short)j,(short)(len*400)); HSSFRichTextString srts=new HSSFRichTextString(value); HSSFCell hc=hr.createCell((short)j); // hc.setEncoding((short)1); hc.setCellStyle(style1); hc.setCellValue(srts); } else { String value = ""; if ( (String) tm.getValueAt(i - 1, j) == null) { value = ""; } else { value = tm.getValueAt(i - 1, j).toString(); } HSSFRichTextString srts=new HSSFRichTextString(value); HSSFCell hc=hr.createCell((short)j); // hc.setEncoding((short)1); hc.setCellStyle(style); if(value.equals("")||value==null) { hc.setCellValue(new HSSFRichTextString("")); } else { hc.setCellValue(srts); } } } } try { wb.write(fos); fos.close(); } catch (IOException ex) { ex.printStackTrace(); } }} 下载一个poi 传递一个jtable 就直接打印了package com.fox_ice.TestDB;import java.awt.*;import javax.swing.*;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import javax.swing.table.TableModel;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.hssf.usermodel.HSSFFont;import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import java.io.FileOutputStream;public class Ipexcel extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); JTable jtable; public Ipexcel(JTable jtable) { try { this.jtable=jtable; setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Component initialization. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(borderLayout1); setSize(new Dimension(400, 300)); setTitle("Frame Title"); FileDialog fd=new FileDialog(this,"选择保存文件的地址", FileDialog.SAVE); fd.setFile("这里修改文件名.xls"); fd.show();String strFile=fd.getDirectory()+fd.getFile();/* JFileChooser chooser = new JFileChooser(".");File s=chooser.getCurrentDirectory() ;String s1=s+"";chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);JavaFileFilter javaFilter = new JavaFileFilter();chooser.addChoosableFileFilter(javaFilter);chooser.setFileFilter(javaFilter);int rs = chooser.showSaveDialog(Main.this); */FileOutputStream fos = new FileOutputStream(strFile);// JFileChooser jfc=new JFileChooser(); HSSFWorkbook wb=new HSSFWorkbook(); HSSFSheet hs=wb.createSheet(); TableModel tm=jtable.getModel(); int row=tm.getRowCount(); int cloumn=tm.getColumnCount(); HSSFCellStyle style=wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short)11); style.setFont(font); HSSFCellStyle style1=wb.createCellStyle(); style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); style1.setBorderLeft(HSSFCellStyle.BORDER_THIN); style1.setBorderRight(HSSFCellStyle.BORDER_THIN); style1.setBorderTop(HSSFCellStyle.BORDER_THIN); style1.setFillForegroundColor(HSSFColor.ORANGE.index); style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font1 = wb.createFont(); font1.setFontHeightInPoints((short)15); font1.setBoldweight((short)700); style1.setFont(font); for(int i=0;i<row+1;i++) { HSSFRow hr=hs.createRow(i); for(int j=0;j<cloumn;j++) { if(i==0) { String value=tm.getColumnName(j); int len=value.length(); hs.setColumnWidth((short)j,(short)(len*400)); HSSFRichTextString srts=new HSSFRichTextString(value); HSSFCell hc=hr.createCell((short)j); // hc.setEncoding((short)1); hc.setCellStyle(style1); hc.setCellValue(srts); } else { String value = ""; if ( (String) tm.getValueAt(i - 1, j) == null) { value = ""; } else { value = tm.getValueAt(i - 1, j).toString(); } HSSFRichTextString srts=new HSSFRichTextString(value); HSSFCell hc=hr.createCell((short)j); // hc.setEncoding((short)1); hc.setCellStyle(style); if(value.equals("")||value==null) { hc.setCellValue(new HSSFRichTextString("")); } else { hc.setCellValue(srts); } } } } try { wb.write(fos); fos.close(); } catch (IOException ex) { ex.printStackTrace(); } }} 新手的困惑,求各路大神解答!在线等~~~ 谁帮老弟看看这个问题 关于实例化对象的问题 JTextArea滚到文本位置问题 版本控制工具用什么好? 垃圾回收器 想用java与pop3邮件服务器连接,只是收邮件。我应该用jmail还是javamail, 它们是一样的嘛?(必结分) OSGI类加载问题 equals方法 数据库中字符串char(n)的定义? ant 中的taskdef 有没有办法向java类传递参数 全屏的问题!
jxl必须这样的
jxl其实比PIO容易学
可以生成Excel的
打算尝试
import javax.swing.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import javax.swing.table.TableModel;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import java.io.FileOutputStream;public class Ipexcel
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JTable jtable;
public Ipexcel(JTable jtable) {
try {
this.jtable=jtable;
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
FileDialog fd=new FileDialog(this,"选择保存文件的地址",
FileDialog.SAVE);
fd.setFile("这里修改文件名.xls");
fd.show();
String strFile=fd.getDirectory()+fd.getFile();
/* JFileChooser chooser = new JFileChooser(".");
File s=chooser.getCurrentDirectory() ;
String s1=s+"";
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
JavaFileFilter javaFilter = new JavaFileFilter();
chooser.addChoosableFileFilter(javaFilter);
chooser.setFileFilter(javaFilter);
int rs = chooser.showSaveDialog(Main.this); */
FileOutputStream fos = new FileOutputStream(strFile);
// JFileChooser jfc=new JFileChooser(); HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet hs=wb.createSheet();
TableModel tm=jtable.getModel();
int row=tm.getRowCount();
int cloumn=tm.getColumnCount();
HSSFCellStyle style=wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)11);
style.setFont(font);
HSSFCellStyle style1=wb.createCellStyle();
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setFillForegroundColor(HSSFColor.ORANGE.index);
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short)15);
font1.setBoldweight((short)700);
style1.setFont(font); for(int i=0;i<row+1;i++)
{
HSSFRow hr=hs.createRow(i);
for(int j=0;j<cloumn;j++)
{
if(i==0)
{
String value=tm.getColumnName(j);
int len=value.length();
hs.setColumnWidth((short)j,(short)(len*400));
HSSFRichTextString srts=new HSSFRichTextString(value);
HSSFCell hc=hr.createCell((short)j);
// hc.setEncoding((short)1);
hc.setCellStyle(style1);
hc.setCellValue(srts);
}
else
{
String value = "";
if ( (String) tm.getValueAt(i - 1, j) == null) {
value = "";
}
else {
value = tm.getValueAt(i - 1, j).toString();
} HSSFRichTextString srts=new HSSFRichTextString(value);
HSSFCell hc=hr.createCell((short)j);
// hc.setEncoding((short)1);
hc.setCellStyle(style); if(value.equals("")||value==null)
{
hc.setCellValue(new HSSFRichTextString(""));
}
else
{
hc.setCellValue(srts);
}
}
}
} try
{
wb.write(fos);
fos.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
import javax.swing.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import javax.swing.table.TableModel;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import java.io.FileOutputStream;public class Ipexcel
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JTable jtable;
public Ipexcel(JTable jtable) {
try {
this.jtable=jtable;
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
FileDialog fd=new FileDialog(this,"选择保存文件的地址",
FileDialog.SAVE);
fd.setFile("这里修改文件名.xls");
fd.show();
String strFile=fd.getDirectory()+fd.getFile();
/* JFileChooser chooser = new JFileChooser(".");
File s=chooser.getCurrentDirectory() ;
String s1=s+"";
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
JavaFileFilter javaFilter = new JavaFileFilter();
chooser.addChoosableFileFilter(javaFilter);
chooser.setFileFilter(javaFilter);
int rs = chooser.showSaveDialog(Main.this); */
FileOutputStream fos = new FileOutputStream(strFile);
// JFileChooser jfc=new JFileChooser(); HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet hs=wb.createSheet();
TableModel tm=jtable.getModel();
int row=tm.getRowCount();
int cloumn=tm.getColumnCount();
HSSFCellStyle style=wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)11);
style.setFont(font);
HSSFCellStyle style1=wb.createCellStyle();
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setFillForegroundColor(HSSFColor.ORANGE.index);
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short)15);
font1.setBoldweight((short)700);
style1.setFont(font); for(int i=0;i<row+1;i++)
{
HSSFRow hr=hs.createRow(i);
for(int j=0;j<cloumn;j++)
{
if(i==0)
{
String value=tm.getColumnName(j);
int len=value.length();
hs.setColumnWidth((short)j,(short)(len*400));
HSSFRichTextString srts=new HSSFRichTextString(value);
HSSFCell hc=hr.createCell((short)j);
// hc.setEncoding((short)1);
hc.setCellStyle(style1);
hc.setCellValue(srts);
}
else
{
String value = "";
if ( (String) tm.getValueAt(i - 1, j) == null) {
value = "";
}
else {
value = tm.getValueAt(i - 1, j).toString();
} HSSFRichTextString srts=new HSSFRichTextString(value);
HSSFCell hc=hr.createCell((short)j);
// hc.setEncoding((short)1);
hc.setCellStyle(style); if(value.equals("")||value==null)
{
hc.setCellValue(new HSSFRichTextString(""));
}
else
{
hc.setCellValue(srts);
}
}
}
} try
{
wb.write(fos);
fos.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}