java操作poi怎么更改excel中的数据
2018-07-26 · 百度知道合伙人官方认证企业
可以添加spire.xls.jar为依赖,来读写excel
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ModifyExcel {
public static void main(String[] args) {
//创建Workbook对象
Workbook wb = new Workbook();
//加载Excel文档
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0);
//更改单元格A1的值
sheet.getCellRange("A1").setText("新文本");
//保存文档
wb.saveToFile("NewDocument.xlsx", ExcelVersion.Version2016);
}
}
推荐于2018-05-17 · 知道合伙人数码行家
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class ChangeCell {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
String fileToBeRead = "C:\\exp.xls"; // excel位置
int coloum = 1; // 比如你要获取第1列
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
HSSFSheet sheet = workbook.getSheet("Sheet1");
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow((short) i);
if (null == row) {
continue;
} else {
HSSFCell cell = row.getCell((short) coloum);
if (null == cell) {
continue;
} else {
System.out.println(cell.getNumericCellValue());
int temp = (int) cell.getNumericCellValue();
cell.setCellValue(temp + 1);
}
}
}
FileOutputStream out = null;
try {
out = new FileOutputStream(fileToBeRead);
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
广告 您可能关注的内容 |