如何用java来打印excel

如何用java来打印excel,第1张

import javaioFile;
 
import jxlWorkbook;
import jxlwriteLabel;
import jxlwriteWritableSheet;
import jxlwriteWritableWorkbook;
 
/
  Excel导出(你需要引入jxl的jar包)
 /
public class Test {
 
    public static void main(String[] args)
    {
        Test test = new Test();
        testexportExcel();
    }
 
    /
      导出(导出到磁盘)
     /
    public void exportExcel() {
        WritableWorkbook book = null;
        try {
            // 打开文件
            book = WorkbookcreateWorkbook(new File("D:/测试xls"));
            // 生成名为"学生"的工作表,参数0表示这是第一页
            WritableSheet sheet = bookcreateSheet("学生", 0);
            // 指定单元格位置是第一列第一行(0, 0)以及单元格内容为张三
            Label label = new Label(0, 0, "张三");
            // 将定义好的单元格添加到工作表中
            sheetaddCell(label);
            // 保存数字的单元格必须使用Number的完整包路径
            jxlwriteNumber number = new jxlwriteNumber(1, 0, 30);
            sheetaddCell(number);
            // 写入数据并关闭文件
            bookwrite();
        } catch (Exception e) {
            Systemoutprintln(e);
        }finally{
            if(book!=null){
                try {
                    bookclose();
                } catch (Exception e) {
                    eprintStackTrace();
                } 
            }
        }
    }
}

如果是导出成表格,使用,分隔的csv文件就方便,,,,,,,否则,使用POI导出成EXCEL格式的
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~
~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~

网上好多啊 我给你找了一个例子 view plaincopy to clipboardprint
public static void main(String[] args) {
try {
String filepath = "d:\\问题清单xls";
FileInputStream fis = new FileInputStream(filepath);
// POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("d:\\OA问题清单xls"));
// HSSFWorkbook hwb = new HSSFWorkbook(pfs);
HSSFWorkbook hwb = new HSSFWorkbook(fis);
// HSSFSheet hws = hwbgetSheetAt(0);
HSSFSheet hws = hwbgetSheet("问题清单");
HSSFRow row = hwsgetRow(2);
HSSFCell cell = null;
cell = rowgetCell((short) 1);
Systemoutprintln("cellnumber:"+cellgetCellNum());
Systemoutprintln("cellvalue:"+getExcelCellValue(cell));
cellsetCellValue(new Date());
cell = rowgetCell((short)2);
HSSFRichTextString rts = new HSSFRichTextString("输入");
cellsetCellValue(rts);
FileOutputStream fos = new FileOutputStream(filepath);
hwbwrite(fos);
fisclose();
fosclose();
} catch (FileNotFoundException ex) {
LoggergetLogger(poiclassgetName())log(LevelSEVERE, null, ex);
} catch (IOException ex) {
LoggergetLogger(poiclassgetName())log(LevelSEVERE, null, ex);
}

}

public static String getExcelCellValue(HSSFCell cell) {
String ret = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DecimalFormat df = new DecimalFormat("#");
int celltype = cellgetCellType();
switch(celltype){
case HSSFCellCELL_TYPE_NUMERIC:
if(HSSFDateUtilisCellDateFormatted(cell)){
ret = sdfformat(cellgetDateCellValue());
}else{
ret = dfformat(cellgetNumericCellValue());
}
break;
case HSSFCellCELL_TYPE_STRING:
ret = cellgetRichStringCellValue()toString();
break;
default:

}
return ret;
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13409860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-30
下一篇 2023-07-30

发表评论

登录后才能评论

评论列表(0条)

保存