在Java中如何实现word的插入功能

2024-12-22 12:00:26
推荐回答(1个)
回答1:

你所谓的插入word是将整个word文档插入数据库还是??
还是先生成word文档 再插入数据
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.BLOB;

public class OracleBlob {
private DBstep.iDBManager2000 DbaObj ;
private File file = null;

public OracleBlob() {
DbaObj=new DBstep.iDBManager2000(); //创建数据库对象
}

public void insert(String infile) throws Exception {
FileInputStream fis = null;
int iFileId = -1;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//生成随机码
java.util.Date dt=new java.util.Date();
long lg=dt.getTime();
Long ld=new Long(lg);
String mRecordID=ld.toString();
file = new File(infile);
fis = new FileInputStream(file);
//得到数据库
iFileId = DbaObj.GetMaxID("INFO_DOCUMENT_FILE","FileId");
if (DbaObj.OpenConnection()) {
DbaObj.Conn.setAutoCommit(false);
String sql="insert into INFO_DOCUMENT_FILE(FileID,RecordID,FileName,FileType,FileSize,FileDate,FileBody,FilePath,UserName,Descript) values (?,?,?,?,?,?,empty_blob(),?,?,? )";
pstmt = DbaObj.Conn.prepareStatement(sql);
pstmt.setInt(1, iFileId);
pstmt.setString(2, mRecordID);
pstmt.setString(3, mRecordID+".doc");
pstmt.setString(4, "doc");
pstmt.setLong(5,file.length());
pstmt.setDate(6, DbaObj.GetDate());
pstmt.setString(7, "");
pstmt.setString(8, "");
pstmt.setString(9, "测试通用版本=="); // "通用版本"
pstmt.executeUpdate();
pstmt = DbaObj.Conn.prepareStatement("select FileBody,FileSize from INFO_DOCUMENT_FILE where RecordID='"+mRecordID+"' for update");
rs = pstmt.executeQuery();
while (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("FileBody");
OutputStream os = blob.getBinaryOutputStream();
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buf = new byte[rs.getInt("FileSize")];
int len = 0;
if ((len = bis.read(buf)) != -1) {
os.write(buf, 0, len);
os.flush();
}
os.close();
bis.close();
}
DbaObj.Conn.commit();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
fis.close();
DbaObj.CloseConnection();
}
}

public void readDoc(String outfile,String id)throws Exception{
FileOutputStream fos = null;
InputStream is = null;
String Sql = "SELECT FileBody,FileSize FROM info_document_file WHERE RecordID='" + id + "'";
try {
if (DbaObj.OpenConnection()) {

try {
ResultSet result = DbaObj.ExecuteQuery(Sql);
if (result.next()) {
try {
int fileSize=result.getInt("FileSize");
Blob blob=result.getBlob("FileBody");
file = new File(outfile);
if(!file.exists())
{
file.createNewFile();//如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = blob.getBinaryStream();// 读出数据后转换为二进制流
byte[] data = new byte[fileSize];
int size = 0;
while((size = is.read(data)) != -1)
{
fos.write(data,0,size);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
result.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
finally
{
DbaObj.CloseConnection();
}
}

public static void main(String args[])
{
OracleBlob blob=new OracleBlob();
try {
blob.readDoc("c://test.doc", "1281088075378");
//blob.insert("c://信息处WebService接口文档.doc");
} catch (Exception e) {
e.printStackTrace();
}
}
}