excel文件转换成二进制文件 大家好,请问怎么把一个二进制的文件转换成excel文件,把一个excel文件转换成二进制文件。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可用一般的fread,fwrite,fopen或FileCreate,FileRead,FileWrite即可。 先读出Excel中的内容,然后写文件。 excel一般是一个字段记录位置,一个字段记录长度,一个字段记录内容,把这些转换成二进制,最后一起生成一个文件即可. private void insertIt() throws FileNotFoundException,java.sql.SQLException { java.sql.Connection con = null; java.sql.PreparedStatement pstmt = null; String SQL = null; FileInputStream fin=null; SQL="INSERT INTO dbo.CASE_ATTACHMENT (HELPDESK_NO, SEQ_NO, FILE_NAME,FILE_CONTENT, CREATED_BY, CREATED_DATE ) VALUES (?, ?, ?, ?, ?, ? )"; try { File f = new File(ContentFileName); fin = new FileInputStream(f); int len = (int)f.length(); con = getCon(); int id=1; pstmt = con.prepareStatement(SQL); pstmt.setString(id++, HelpdeskNO); SeqNO=getNextSeqNO(); pstmt.setInt(id++, SeqNO); pstmt.setString(id++, f.getName()); pstmt.setBinaryStream(id++, fin,len); pstmt.setString(id++, CreatedBy); if(CreatedDate!=null) pstmt.setTimestamp(id++, CreatedDate.getSQLTimestamp()); else pstmt.setTimestamp(id++, null) ; int iAffected = pstmt.executeUpdate(); //to do commit(); setIsNew(false); } catch (SQLException sqle) { try { //if you need.. rollback(); } catch (Exception e) {} System.err.println("Error : " + sqle); sqle.printStackTrace(); throw sqle; } finally { try {pstmt.close();} catch (Exception e) {} try {closeCon();} catch (Exception e) {} try{ fin.close();}catch(Exception e){} } }此Function 是将文件"ContentFileName" 以二进制存入数据库 将二进制数据从数据库读出 产生excel文件 //masterObject=home.getCase(helpDeskNo); int seq=hr.getInt("seq"); String fileName=hr.getString("filename");// CaseAttachmentHome attachHome= HomeFactory.getCaseAttachmentHome(con);// CaseAttachment attachment=attachHome.getCaseAttachment(helpDeskNo,seq);// if(!attachment.getFileName().equals(fileName))return; java.sql.PreparedStatement pstmt = null; OutputStream fileOut=null; String SQL = null; Pageable rs = null; SQL="SELECT FILE_CONTENT FROM dbo.CASE_ATTACHMENT WHERE HELPDESK_NO=? and SEQ_NO=? "; try { int id=1; pstmt = con.prepareStatement(SQL); pstmt.setString(id++, helpDeskNo); pstmt.setInt(id++, seq); rs = (Pageable) pstmt.executeQuery(); if(rs.getRowsCount()==0) throw new CaseAttachmentNotFoundException(this); rs.first(); InputStream fc = rs.getBinaryStream("FILE_CONTENT"); log.info("size="+fc.available()); response.reset(); response.setContentType("application/x-download"); response.addHeader( "Content-Disposition", "attachment; filename=" + fileName); byte[] b = new byte[fc.available()]; int rec =0;// OutputStream sout = new FileOutputStream(fileName); ServletOutputStream sout = response.getOutputStream();// w fc.read(b,0,b.length);// w sout.write(b); while ((rec = fc.read(b)) > 0) { sout.write(b,0,rec); } // w while(fc.read(b)!=-1){// w fileOut.write(b);// w }// fileOut.close(); sout.close(); fc.close(); rs.close();// fileOut = new FileOutputStream(fileName);// byte[] idata = new byte[8000];//// response.setCharacterEncoding("utf-8");// response.setContentType("application/x-download");// response.addHeader("Accept-Ranges", "bytes");// response.addHeader("Accept-Length", "" + fc.available());// response.addHeader(// "Content-Disposition",// "attachment; filename=" + fileName);//// int rec =0;// while ((rec = fc.read(idata)) > 0) {// fileOut.write(idata,0,rec);// }//// while(fc.read(idata)!=-1){//// fileOut.write(idata);//// }//// idata = null;// fileOut.flush();// fileOut.close();// fc.close(); } catch (SQLException sqle) { //to do System.err.println("Error : " + sqle); sqle.printStackTrace(); throw sqle; } finally { try {rs.close();} catch (Exception e) {} try {pstmt.close();} catch (Exception e) {} try {fileOut.close();} catch (Exception e) {} } 急求 韩顺平的时尚购物网是模仿的哪个网站 求素材 JavaScript 图片预读取问题??????????????? 表达式语言只能写在标签里吗? 帮我解决一个图像刷新的问题 文件上传 为什么拿不到文件 时间问题 斑竹请进,(其他兄弟也可以帮忙!谢谢了) jsp中传值问题 用JDBC-ODBC连接SQLServer7的问题,请大虾多多指点。 关于一个log4j路径的问题的问题 struts2注销session问题 标签问题 (在线等) 关于在<html:select>标签中插入java代码
先读出Excel中的内容,然后写文件。
java.sql.Connection con = null;
java.sql.PreparedStatement pstmt = null;
String SQL = null;
FileInputStream fin=null;
SQL="INSERT INTO dbo.CASE_ATTACHMENT (HELPDESK_NO, SEQ_NO, FILE_NAME,FILE_CONTENT, CREATED_BY, CREATED_DATE ) VALUES (?, ?, ?, ?, ?, ? )";
try {
File f = new File(ContentFileName);
fin = new FileInputStream(f);
int len = (int)f.length(); con = getCon();
int id=1;
pstmt = con.prepareStatement(SQL);
pstmt.setString(id++, HelpdeskNO);
SeqNO=getNextSeqNO();
pstmt.setInt(id++, SeqNO);
pstmt.setString(id++, f.getName());
pstmt.setBinaryStream(id++, fin,len); pstmt.setString(id++, CreatedBy);
if(CreatedDate!=null)
pstmt.setTimestamp(id++, CreatedDate.getSQLTimestamp());
else
pstmt.setTimestamp(id++, null)
;
int iAffected = pstmt.executeUpdate();
//to do
commit();
setIsNew(false);
}
catch (SQLException sqle) {
try {
//if you need..
rollback();
}
catch (Exception e) {}
System.err.println("Error : " + sqle);
sqle.printStackTrace();
throw sqle;
}
finally {
try {pstmt.close();} catch (Exception e) {}
try {closeCon();} catch (Exception e) {}
try{ fin.close();}catch(Exception e){}
}
}此Function 是将文件"ContentFileName" 以二进制存入数据库
int seq=hr.getInt("seq");
String fileName=hr.getString("filename");// CaseAttachmentHome attachHome= HomeFactory.getCaseAttachmentHome(con);
// CaseAttachment attachment=attachHome.getCaseAttachment(helpDeskNo,seq);
// if(!attachment.getFileName().equals(fileName))return; java.sql.PreparedStatement pstmt = null;
OutputStream fileOut=null;
String SQL = null;
Pageable rs = null; SQL="SELECT FILE_CONTENT FROM dbo.CASE_ATTACHMENT WHERE HELPDESK_NO=? and SEQ_NO=? ";
try {
int id=1;
pstmt = con.prepareStatement(SQL); pstmt.setString(id++, helpDeskNo);
pstmt.setInt(id++, seq); rs = (Pageable) pstmt.executeQuery(); if(rs.getRowsCount()==0) throw new CaseAttachmentNotFoundException(this); rs.first();
InputStream fc = rs.getBinaryStream("FILE_CONTENT");
log.info("size="+fc.available());
response.reset();
response.setContentType("application/x-download");
response.addHeader(
"Content-Disposition",
"attachment; filename=" + fileName);
byte[] b = new byte[fc.available()];
int rec =0;
// OutputStream sout = new FileOutputStream(fileName);
ServletOutputStream sout = response.getOutputStream();
// w fc.read(b,0,b.length);
// w sout.write(b);
while ((rec = fc.read(b)) > 0) {
sout.write(b,0,rec);
}
// w while(fc.read(b)!=-1){
// w fileOut.write(b);
// w }
// fileOut.close();
sout.close();
fc.close();
rs.close();// fileOut = new FileOutputStream(fileName);
// byte[] idata = new byte[8000];
//
// response.setCharacterEncoding("utf-8");
// response.setContentType("application/x-download");
// response.addHeader("Accept-Ranges", "bytes");
// response.addHeader("Accept-Length", "" + fc.available());
// response.addHeader(
// "Content-Disposition",
// "attachment; filename=" + fileName);
//
// int rec =0;
// while ((rec = fc.read(idata)) > 0) {
// fileOut.write(idata,0,rec);
// }
//// while(fc.read(idata)!=-1){
//// fileOut.write(idata);
//// }
//
// idata = null;
// fileOut.flush();
// fileOut.close();
// fc.close();
} catch (SQLException sqle) {
//to do
System.err.println("Error : " + sqle);
sqle.printStackTrace();
throw sqle;
}
finally {
try {rs.close();} catch (Exception e) {}
try {pstmt.close();} catch (Exception e) {}
try {fileOut.close();} catch (Exception e) {}
}