返回目录:word文档
可以用poi先将word下载到本地,
在用Runtime.getRuntime().exec(); 调用本地文件e799bee5baa6e79fa5e98193e78988e69d83363
package cn.rain.main;
import java.io.File;
import java.io.IOException;
public class TT {
/**
* @param args
*/
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE aa.doc");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
你的WORD安装路径C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE空格后所加的aa.doc为你的文档文件名(有文件名重复的,可以加路径,写法和安装路径的写法一样)
或者用Runtime.getRuntime().exec("cmd /c \"C:\\temp\\the list.doc\"");
也可以运行
使用java中的来io进行读取源
BufferedReader bufferedReader = null;
File file = new File("文档地址+文档名.docx");
if(!file.exists()){
System.out.println("文件不存在百");
} else {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字度符格式(UTF-8或GBK)"));
String lineText = null;
while((lineText = bufferedReader.readLine()) != null){
if (linText != null && !lineText.eq("")){
System.out.println("一次读取一行知,一行内道容为:" + lineText);
}
}
}
文件e79fa5e98193e59b9ee7ad94363从本地到服务器的功能,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。
java中文件上传到服务器的指定路径的代码:
在前台界面中输入:
<form method="post" enctype="multipart/form-data" action="../manage/excelImport.do">
请选文件:<input type="file" name="excelFile">
<input type="submit" value="导入" onclick="return impExcel();"/>
</form>
action中获取前台传来数据并保存
/**
* excel 导入文件
* @return
* @throws IOException
*/
@RequestMapping("/usermanager/excelImport.do")
public String excelImport(
String filePath,
MultipartFile excelFile,HttpServletRequest request) throws IOException{
log.info("<<<<<<action:{} Method:{} start>>>>>>","usermanager","excelImport" );
if (excelFile != null){
String filename=excelFile.getOriginalFilename();
String a=request.getRealPath("u/cms/www/201509");
SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服务器的路径
}
log.info("<<<<<<action:{} Method:{} end>>>>>>","usermanager","excelImport" );
return "";
}
/**
* 将MultipartFile转化为file并保存到服务器上的某地
*/
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
{
FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);
System.out.println("------------"+path + "/"+ savefile);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
你把它当普通文件来上传怎么会有格式丢失的问题呢?
你要是用一个富文本编辑界面去复制粘贴肯定是会丢掉很多样式的