作者:乔山办公网日期:
返回目录:excel表格制作
思路为:为了防止数字变成科学计zhidao数法方式表示,在源文件以及java代码中都用文本的方式去生成和解析excel,具体回如下: 1.生成Excel时,设置单元格格答式为STRING,即: //关键代码HSSFCell cell = new HSSFCell();cell.setCellType(HSSFCell.CELL_TYPE_STRING);2.同理,解析的时候,首先要保证源excel文件中该单元格格式是文本类型的,然后在java代码里用STRING类型去解析: //关键代码String value = cell.getStringCellValue()。
修改完需要写入,也就是保存一下的。
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; // 比如你要获7a64e58685e5aeb9333取第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();
}
}
}
如果你用的是POI插件
Row row = sheet.getRow(0);
row.setHeightInPoints(30);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
然后使百用度row.setHeight()或者内row.setHeightInPoints()来调整行高容