我在EXCEL的一个sheet的第0列,第0~4行,这4个单元格首先在EXCEL内手动设成右对齐。
然后想用POI来改变A2这个单元格为左对齐,A3这个单元格设为居中对齐,如下代码:
HSSFRow row1 = sheet0.getRow(1);
HSSFCell cell1 = row1.getCell(0);
HSSFCellStyle cellStyle1 = cell1.getCellStyle();
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cell1.setCellStyle(cellStyle1);
HSSFRow row2 = sheet0.getRow(2);
HSSFCell cell2 = row2.getCell(0);
HSSFCellStyle cellStyle2 = cell2.getCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell2.setCellStyle(cellStyle2);但是结果是这4个单元格都居中对齐了,请问这是怎么回事?我只是改变的A2、A3这两个单元格的格式啊???而且A2这个单元格也没变成左对齐啊??
请高人指点,谢谢
然后想用POI来改变A2这个单元格为左对齐,A3这个单元格设为居中对齐,如下代码:
HSSFRow row1 = sheet0.getRow(1);
HSSFCell cell1 = row1.getCell(0);
HSSFCellStyle cellStyle1 = cell1.getCellStyle();
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cell1.setCellStyle(cellStyle1);
HSSFRow row2 = sheet0.getRow(2);
HSSFCell cell2 = row2.getCell(0);
HSSFCellStyle cellStyle2 = cell2.getCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell2.setCellStyle(cellStyle2);但是结果是这4个单元格都居中对齐了,请问这是怎么回事?我只是改变的A2、A3这两个单元格的格式啊???而且A2这个单元格也没变成左对齐啊??
请高人指点,谢谢
HSSFCell cell1 = row1.getCell(0);
HSSFCellStyle cellStyle1 = cell1.getCellStyle();
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cell1.setCellStyle(cellStyle1);
HSSFRow row2 = sheet0.getRow(2);
HSSFCell cell2 = row2.getCell(0);
HSSFCellStyle cellStyle2 = cell2.getCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell2.setCellStyle(cellStyle2);这里处理的是同一列 当然是都居中了
HSSFCell cell1 = row1.getCell(0);
HSSFCellStyle cellStyle1 = cell1.getCellStyle(); //获取cell1的style HSSFCellStyle t = hssfworkbook.createCellStyle(); //创建新的style
t.cloneStyleFrom(cellStyle1); //拷贝旧的style
t.setAlignment(HSSFCellStyle.ALIGN_LEFT);//然后在新的style上设置对齐方式 cell1.setCellStyle(t);//应用新的style,这样新的样式只应用于此单元格。按照楼主的那方法,会应用一列。楼主可参考我的方法。我测试过了,没问题。
就根本没有这个方法啊~~~~~~~~~
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.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class HelpExcelAlign { /**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("c:\\test.xls");
HSSFWorkbook hssfworkbook = new HSSFWorkbook(fis);
fis.close();
HSSFSheet sheet0 = hssfworkbook.getSheetAt(0);
HSSFRow row1 = sheet0.getRow(1);
HSSFCell cell1 = row1.getCell(0);
HSSFCellStyle cellStyle1 = cell1.getCellStyle();
HSSFCellStyle t = hssfworkbook.createCellStyle();
t.cloneStyleFrom(cellStyle1);
t.setAlignment(HSSFCellStyle.ALIGN_LEFT);
//cellStyle1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cell1.setCellStyle(t);
HSSFRow row2 = sheet0.getRow(2);
HSSFCell cell2 = row2.getCell(0);
HSSFCellStyle cellStyle2 = cell2.getCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cell2.setCellStyle(cellStyle2);
FileOutputStream fos = new FileOutputStream("c:\\test.xls");
hssfworkbook.write(fos);
fos.close(); } public static void diGui(String path) {
File f = new File(path);
if (f.isDirectory()) {
System.out.println(f.getPath()); //打印目录路径
File[] l = f.listFiles();
for (int x = 0; x < l.length; x++) {
diGui(l[x].getPath()); //递归
}
} else if (f.isFile()) {
System.out.println(f.getPath());//打印文件路径
}
}
}