标签:
package excelDemo1; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.text.DecimalFormat; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * java读取excel文件及内容 * * @author tuzongxun123 * */ public class ExcelDemo1 { public static void main(String[] args) { try { // 获取文件在磁盘的存储路径 File file = new File("C:" + File.separator + "Users" + File.separator + "tuzongxun123" + File.separator + "Desktop" + File.separator + "log.xls"); // 读文件输入流 FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream( fileInputStream); POIFSFileSystem excelfile = new POIFSFileSystem(bufferedInputStream); HSSFWorkbook workbook = new HSSFWorkbook(excelfile); // 根据名称获取excel工作薄 HSSFSheet sheet = workbook.getSheet("Sheet0"); // 遍历去除工作薄中的行和列 for (int i = 1;; i++) { HSSFRow row = sheet.getRow(i); if (row != null) { // 获取科学记数法这一列的单元格 HSSFCell cell = row.getCell(6); // 格式化科学计数法的数据格式 DecimalFormat df = new DecimalFormat("0"); String value = df.format(cell.getNumericCellValue()); // 时间转换 long long1 = Long.parseLong(value); Date date = new Date(long1); // 创建新的单元格存储转换后的数据 HSSFCell cell2 = row.createCell(9); cell2.setCellValue(date); } else { break; } } // 把转换后的数据写入到文件中 OutputStream outputStream = new FileOutputStream(file); workbook.write(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }
标签:
原文地址:http://blog.csdn.net/tuzongxun/article/details/51386248