java 给excel添加一列_java操作excel在开始位置添加一列
首先說一下,我用的JXL(JXL比較簡(jiǎn)單,但是沒有POI功能強(qiáng)大):
jar包:jxl.jar;
沒有什么直接的方法可以實(shí)現(xiàn)這個(gè)功能,我用的是把原先的excel中的內(nèi)容取出來,放入新的excel,但是從第二列開始存放,從而達(dá)到效果,下面是代碼...
package Excel;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.File;
/*** Created by huipu on 2016/11/10.*/public class Excel {
public static void main(String[] args) {
String url = "C:/Users/huipu/Desktop/表.xls";
InsertExcelClo(url);
}
public static void InsertExcelClo(String url){
File file = new File(url);
if(!file.exists()){
System.out.println("文件不存在");
}
Workbook wb = null;
WritableWorkbook wwb = null;
try{
wb = Workbook.getWorkbook(file);
wwb = Workbook.createWorkbook(file);
Sheet old_sheet = wb.getSheet(0);
String old_name = old_sheet.getName();
int cols = old_sheet.getColumns();
int rows = old_sheet.getRows();
WritableSheet new_sheet = wwb.createSheet(old_name,0);
for(int i = 0;i
for(int j = 0;j
if(j==0){
if(i==0){
Label label = new Label(0,0,"xx");
new_sheet.addCell(label);
}else if(i==1){
Label label = new Label(0,1,"xxx");
new_sheet.addCell(label);
}else {
Label label = new Label(0,i,"");
new_sheet.addCell(label);
}
}else {
Cell cell = old_sheet.getCell(j-1,i);
String str = cell.getContents().equals("")?"":cell.getContents();
Label label = new Label(j,i,str);
new_sheet.addCell(label);
}
}
}
wwb.write();
System.out.println("修改成功");
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(wb != null){
wb.close();
}if(wwb != null){
wwb.close();
}
}catch (Exception e){
System.out.println("關(guān)閉異常");
e.printStackTrace();
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java 给excel添加一列_java操作excel在开始位置添加一列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑无法连接mysql_本地电脑无法连接
- 下一篇: Docker run 容器处于creat