根据Excel单元格坐标修改单元格内的Value

package com.haier.demo.maven_web_demo.grid2.main;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class PoiTestOf2007 {
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis(); // 获取开始时间
String currentIndex = "I47";
String one = null;


String two = null;
if (currentIndex.length() == 2) {
one = currentIndex.substring(0, 1);
two = currentIndex.substring(1, 2);
}
if (currentIndex.length() == 3) {
one = currentIndex.substring(0, 1);
two = currentIndex.substring(1, 3);
}
if (currentIndex.length() == 4) {
one = currentIndex.substring(0, 2);
two = currentIndex.substring(2, 4);
}


String[] arr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE",
"AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO",
"AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY",
"AZ" };
Map<String, Integer> map = new HashMap<String, Integer>();
int i = 0;
for (String string : arr) {
map.put(string, (++i));
}


int beginCell = map.get(one) - 1;
int beginRow = Integer.parseInt(two) - 1;
System.out.println(beginRow + "======" + beginCell);
String fileToBeRead = "e:/青岛小规模纳税人报表.xlsx"; // excel位置
XSSFWorkbook workbook;
// HSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead));
XSSFSheet sheet = workbook.getSheet("Sheet1");
XSSFRow row = sheet.getRow(beginRow);


// 设置单元格样式
CellStyle style = workbook.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);


XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 8);
style.setFont(font);
// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
if (null == row) {
row = sheet.createRow(beginRow);
}
XSSFCell cell = row.getCell(beginCell);
System.out.println(cell);


if (null == cell) {
cell = row.createCell(beginCell);
}
// 向单元格中放入值
cell.setCellValue("8");
// 设置存入内容为字符串
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(style);


FileOutputStream fileOut = new FileOutputStream(
"e:/青岛小规模纳税人报表.xlsx");
workbook.write(fileOut);
fileOut.close();
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值