POI读写excel实例 (1)
来源:互联网 发布:js获取json字符串数据 编辑:程序博客网 时间:2024/06/03 21:47
excel读入进行单元格赋值
package testPOI;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class TestExcel {public static void main(String[] args) throws IOException { Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook(); FileInputStream io = null; try { io = new FileInputStream("d:/workbook.xlsx"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { wb = new XSSFWorkbook(io); io.close(); } catch (IOException e) { e.printStackTrace(); } Sheet sheet1 = wb.getSheetAt(0); Row row = sheet1.getRow(0); int copyStartColumnIndex = 0; int coyColumnCount = 3; int columnEndIndex = row.getLastCellNum(); for (int i = columnEndIndex; i > copyStartColumnIndex + coyColumnCount - 1; i--) { Cell cellNew = TestExcel.getCell(row, i + coyColumnCount); Cell cellOld = TestExcel.getCell(row, i); TestExcel.copyCell(cellNew, cellOld, sheet1); } for (int i = copyStartColumnIndex; i < coyColumnCount; i++) { Cell cellNew = TestExcel.getCell(row, i + coyColumnCount); Cell cellOld = TestExcel.getCell(row, i); TestExcel.copyCell(cellNew, cellOld, sheet1); } FileOutputStream fileOut = new FileOutputStream("d:/workbook.xlsx"); wb.write(fileOut); fileOut.close();}public static void copyCell(Cell newCell, Cell oldCell, Sheet sheet) {CellStyle oldStyle = oldCell.getCellStyle();newCell.setCellStyle(oldStyle);newCell.setCellValue(oldCell.getStringCellValue());sheet.setColumnWidth(newCell.getColumnIndex(), sheet.getColumnWidth(oldCell.getColumnIndex()));}public static Cell getCell (Row row, int columnNum) {Cell cell = row.getCell(columnNum);if (cell == null) {cell = row.createCell(columnNum);}return cell;}}
0 0
- POI读写excel实例 (1)
- POI读写excel文件实例
- java用poi读写excel实例
- java读写Excel(POI)
- POI读写excel实例和JXL实现excel的方式 (2)
- poi(1)读写excel小例子
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel文件
- POI读写Excel
- POI读写excel文件
- poi读写excel
- poi读写 excel
- Java读写Excel-POI
- 用POI读写Excel
- POI简单读写excel
- SetThreadAffinityMask的用法
- ZOJ 1136 Multiply
- spring scope
- 【PAT】1088. Rational Arithmetic (20)
- python中的pandas包的数据清洗能力
- POI读写excel实例 (1)
- Spark Internals
- Swift 播放视频
- hdu 3727 Jewel(主席树学习第四弹)
- Two Sum
- 【队列项目2 - 建立链队算法库——第7周】
- Cpp--字符串快速查找运用
- window.close无效的js解决方案
- oracle安装完初次连接问题