excel文件转换成二进制文件

windowboy 2008-10-06 04:43:09
大家好,请问怎么把一个二进制的文件转换成excel文件,把一个excel文件转换成二进制文件。
...全文
686 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangsf1982 2008-10-06
  • 打赏
  • 举报
回复
将二进制数据从数据库读出 产生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) {}


}
zhangsf1982 2008-10-06
  • 打赏
  • 举报
回复
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" 以二进制存入数据库
wunan320 2008-10-06
  • 打赏
  • 举报
回复
excel一般是一个字段记录位置,一个字段记录长度,一个字段记录内容,把这些转换成二进制,最后一起生成一个文件即可.
jabeginner 2008-10-06
  • 打赏
  • 举报
回复
可用一般的fread,fwrite,fopen或FileCreate,FileRead,FileWrite即可。
先读出Excel中的内容,然后写文件。
zhj92lxs 2008-10-06
  • 打赏
  • 举报
回复
.

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧