登 录
1.下载jar包:http://poi.apache.org/download.html
2.实例代码
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JOptionPane; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.junit.Test; public class TestPOI { @Test public void generateExcel(){ HSSFWorkbook workbook = new HSSFWorkbook(); //产生工作簿对象 HSSFSheet sheet = workbook.createSheet(); //产生工作表对象 workbook.setSheetName(0, "test");//设置工作表位置和工作表名 HSSFRow row=sheet.createRow(0);//生成一行 HSSFCell cell = row.createCell((short) 0, HSSFCell.CELL_TYPE_STRING);//生成单元格,设定类型 HSSFRichTextString text=new HSSFRichTextString("oh,好耶!"); cell.setCellValue(text); FileOutputStream fOut = null; try { fOut = new FileOutputStream(new File("c:/test.xls")); } catch (FileNotFoundException e) { e.printStackTrace(); } try { workbook.write(fOut); fOut.flush(); fOut.close(); } catch (IOException e) { e.printStackTrace(); } JOptionPane.showMessageDialog(null,"导出数据成功!"); } }
参考:http://www.360doc.com/content/10/0526/14/8667_29610917.shtml
抱歉!评论已关闭.