我希望能将excel里面的内容当作邮件内容发送,有办法实现么?请高手解答,谢谢!

解决方案 »

  1.   

    获取里面的内容,然后利用Java的邮件技术,发送~~~!可以实现
      

  2.   

    谢谢不过我指的是将一个sheet的所有内容发送出去。读excel我用jxl,但它只能读取一个单元格的值。
      

  3.   

    apache的POI是操作excel的
    发送邮件可以使用spring的邮件,也可以使用java的。
    baidu下资料很多。。
      

  4.   

    读取excel,需要jar包 poi.jar 下载地址: http://download.csdn.net/source/2270104import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;/**
     *  @author (版权归原作者)
     * 用于读取excel
     */
    public class ExcelReader {
    private HSSFWorkbook wb = null;// book [includes sheet] private HSSFSheet sheet = null; private HSSFRow row = null; private int sheetNum = 0; // 第sheetnum个工作表 private int rowNum = 0; private FileInputStream fis = null; private File file = null; public ExcelReader() {} public ExcelReader(File file) {
    this.file = file;
    } public void setRowNum(int rowNum) {
    this.rowNum = rowNum;
    } public void setSheetNum(int sheetNum) {
    this.sheetNum = sheetNum;
    } public void setFile(File file) {
    this.file = file;
    } /**
    * 读取excel文件获得HSSFWorkbook对象
    */
    public void open() throws IOException {
    fis = new FileInputStream(file);
    wb = new HSSFWorkbook(new POIFSFileSystem(fis));
    fis.close();
    } /**
    * 返回sheet表数目

    * @return int
    */
    public int getSheetCount() {
    int sheetCount = -1;
    sheetCount = wb.getNumberOfSheets();
    return sheetCount;
    } /**
    * sheetNum下的记录行数
    * @return int
    */
    public int getRowCount() {
    if (wb == null)
        System.out.println("=============>WorkBook为空");
    HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
    int rowCount = -1;
    rowCount = sheet.getLastRowNum();
    return rowCount;
    } /**
    * 读取指定sheetNum的rowCount

    * @param sheetNum
    * @return int
    */
    public int getRowCount(int sheetNum) {
    HSSFSheet sheet = wb.getSheetAt(sheetNum);
    int rowCount = -1;
    rowCount = sheet.getLastRowNum();
    return rowCount;
    } /**
    * 得到指定行的内容
    * @param lineNum
    * @return String[]
    */
    public String[] readExcelLine(int lineNum) {
    return readExcelLine(this.sheetNum, lineNum);
    } /**
    * 指定工作表和行数的内容
    * @param sheetNum
    * @param lineNum
    * @return String[]
    */
    public String[] readExcelLine(int sheetNum, int lineNum) {
    if (sheetNum < 0 || lineNum < 0)
    return null;
    String[] strExcelLine = null;
    try {
    sheet = wb.getSheetAt(sheetNum);
    row = sheet.getRow(lineNum);
    int cellCount = row.getLastCellNum();
    strExcelLine = new String[cellCount + 1];
    for (int i = 0; i <= cellCount; i++) {
    strExcelLine[i] = readStringExcelCell(lineNum, i);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return strExcelLine;
    } /**
    * 读取指定列的内容
    * @param cellNum
    * @return String
    */
    public String readStringExcelCell(int cellNum) {
    return readStringExcelCell(this.rowNum, cellNum);
    } /**
    * 指定行和列编号的内容
    * @param rowNum
    * @param cellNum
    * @return String
    */
    public String readStringExcelCell(int rowNum, int cellNum) {
    return readStringExcelCell(this.sheetNum, rowNum, cellNum);
    } /**
    * 指定工作表、行、列下的内容
    * @param sheetNum
    * @param rowNum
    * @param cellNum
    * @return String
    */
    @SuppressWarnings("deprecation")
    public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
    if (sheetNum < 0 || rowNum < 0)
    return "";
    String strExcelCell = "";
    try {
    sheet = wb.getSheetAt(sheetNum);
    row = sheet.getRow(rowNum); if (row.getCell((short) cellNum) != null) { // add this condition
    // judge
    switch (row.getCell((short) cellNum).getCellType()) {
    case HSSFCell.CELL_TYPE_FORMULA:
    strExcelCell = "FORMULA ";
            break;
        case HSSFCell.CELL_TYPE_NUMERIC: {
         strExcelCell = String.valueOf(row.getCell((short) cellNum)
           .getNumericCellValue());
         }
         break;
        case HSSFCell.CELL_TYPE_STRING:
         strExcelCell = row.getCell((short) cellNum)
         .getStringCellValue();
         break;
        case HSSFCell.CELL_TYPE_BLANK:
         strExcelCell = "";
         break;
        default:
         strExcelCell = "";
         break;
        }
       }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return strExcelCell;
    }
     //主函数用于测试
     public static void main(String args[]) {
     File file = new File("f:\\work\\860_069.xls");
     if (file.exists()) {
     ExcelReader readExcel = new ExcelReader(file);
     try {
     readExcel.open();
     } catch (IOException e) {
     e.printStackTrace();
     }
     int sheetCount = readExcel.getSheetCount();
     for (int s=0; s<sheetCount ;s++) {
     readExcel.setSheetNum(s); // 设置读取索引为0的工作表
     // 总行数
     int count = readExcel.getRowCount();
     for (int i = 0; i <= count; i++) {
     String[] rows = readExcel.readExcelLine(i);
     for (int j = 0; j < rows.length; j++) {
     System.out.print(rows[j] + " ");
     }
     System.out.print("\n");
     }
     System.out.println("--------------------------------------");
     }
     } else {
     System.out.println("此文件不存在!");
     }
     int i=1;
        int a=2;
        int b=3;
        int c=4;
        int e=++i; 
             
        int d=++i + a++ + b++ + c++;
        
        System.out.println(d);
        System.out.println(e); }
    }
    发送email/**
     * @param reply
     *            回复地址
     * @param to
     *            收信人地址
     * @param subject
     *            邮件标题
     * @param email
     *            邮件正文
     */
    public static boolean sendHtmlEmail(String reply, String to,
    String subject, String email) { String smtpServer = Constants.Email_SMTP; // SMTP服务器名
    String name = Constants.Email_LoginName; // 邮箱登录名
    String passWord = Constants.Email_LoginPassword; // 邮箱密码 Properties props = new Properties();
    Session sendMailSession;
    Transport transport;
    sendMailSession = Session.getInstance(props); // props.put("mail.transport.protocol", "smtp");
    // props.put("mail.smtp.starttls.enable","true");
    // props.put("mail.smtp.host", "gmail-smtp.l.google.com");
    // props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", smtpServer);
    props.put("mail.smtp.port", 465);
    // props.put("mail.smtp.port", 465);
    props.put("mail.smtp.auth", "true"); MimeMessage message = new MimeMessage(sendMailSession); try {
    message.setFrom(new InternetAddress("Mingoe<"
    + Constants.Email_Server + ">"));
    // InternetAddress[] ias = new InternetAddress[1];
    // ias[0] = new InternetAddress(reply);
    // message.setReplyTo(ias);
    InternetAddress[] address = InternetAddress.parse(to);
    message.setRecipients(Message.RecipientType.TO, address);
    message.setSubject(subject); // 设置邮件发送时间,将来要和服务器时间互转
    Calendar clientCal = Calendar.getInstance();
    message.setSentDate(clientCal.getTime()); BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
    if (!isYahooEmail(to)) {
    mdp.setContent(email, "text/html;charset=UTF-8"); // 给BodyPart对象设置内容和格式/编码方式
    } else {
    mdp.setContent(email, "text/html;charset=GBK"); // 给BodyPart对象设置内容和格式/编码方式
    }
    Multipart mp = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放BodyPart对象
    mp.addBodyPart(mdp); // 将BodyPart加入到MimeMultipart对象中
    message.setContent(mp); // 把mm作为消息对象的内容 transport = sendMailSession.getTransport("smtp");
    transport.connect(smtpServer, name, passWord);
    message.saveChanges();
    transport.sendMessage(message, message.getAllRecipients());
    transport.close(); return true;
    } catch (AddressException addressException) {
    // addressException.printStackTrace();
    return false;
    } catch (MessagingException messagingException) {
    // messagingException.printStackTrace();
    return false;
    } catch (Exception e) {
    // e.printStackTrace();
    return false;
    }
    }