是不是读取excel内容。是的话如下。不是,抢座
解决方案 »
- struts2.3.4.1 Date数据转换问题
- 关于java单例模式的一些疑问
- 关于preparedstatement中的sql语句中的参数问题
- 跪求一个正则表达式匹配的问题~~急!!!!
- 求救
- 关于中文乱码问题
- lomobz_eclipse浏览器问题
- 一个图片不能正常显示的问题
- Eclipse + Tomcat 的配置问题~~~
- Failed to load Main-Class manifest attribute from D:\Java\hibernate-3.0\lib\xml-apis.jar
- eclipse 远程调试能不能在本地打印控制台的信息
- jnative调用dll Function deCrypt not found的问题。了解的大侠们看看
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.log4j.*;/**
*
* @author Guopengtao
* @date 2007-8-9 Discription 本类是用于将xls文件内容导出到txt文件文件中。内容以|+|分隔
*/
public class ExcelFileToTxt {
static Logger log = Logger.getLogger(ExcelFileToTxt.class);
/**
*
* @param filePath
* @return String[][] xls文件见内容存放的字符串数组
*/
private static String[][] readExcel(String filePath) {
String[][] strTemp = null;
try {
InputStream is = new FileInputStream(filePath);
jxl.Workbook wb = Workbook.getWorkbook(is);//转换警告,暂无定论
Sheet rs = wb.getSheet(0);//默认为第一Sheet
int rows = rs.getRows();
int cols = rs.getColumns();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");// 在1处日期内容按YYYYMMDD格式提取
strTemp = new String[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Cell c = rs.getCell(j, i);
if (c.getType() == CellType.DATE) {
strTemp[i][j] = formatter.format((((DateCell) c)// 1
.getDate()));
} else {
strTemp[i][j] = c.getContents().toString().trim();
}
}
}
wb.close();
} catch (Exception ex) {
log.debug("转换xls文件失败"+ex.getMessage());
}
return strTemp;
}
/**
*
* @param filepath
* @return String 将存放xls文件的字符数组按照|+|格式放入字符串中
*/
public String getFileByte(String filepath) {
String[][] out = null;
out = readExcel(filepath);
int rn = out.length;
int cn;
if (rn > 0)
cn = out[0].length;
else
cn = 0;
String str = "";
for (int i = 0; i < rn; i++) {
for (int j = 0; j < cn; j++) {
str = str + out[i][j].toString().trim();
if (j < cn - 1) {
str = str + "|+|";
}
}
str = str + "\r\n";
}
return str;
}
}
下载jxl.jar