使用POI生成Excel文件,可以自动调整excel列宽
2016-01-21 17:23
585 查看
在开发中经常需要用到对Excel文件的操作,资料如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFCell;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFFont;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFRow;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFSheet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFComment;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class PoiCreateExcelTest {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see <a href="http://poi.apache.org/hssf/quick-guide.html#NewWorkbook">For more</a>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建新的Excel 工作簿
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFWorkbook workbook = new HSSFWorkbook();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFSheet sheet = workbook.createSheet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//HSSFSheet sheet = workbook.createSheet("SheetName");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 用于格式化单元格的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFDataFormat format = workbook.createDataFormat();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建新行(row),并将单元格(cell)放入其中. 行号从0开始计算.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFRow row = sheet.createRow((short) 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFFont font = workbook.createFont();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setFontHeightInPoints((short) 20); //字体高度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setColor(HSSFFont.COLOR_RED); //字体颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setFontName("黑体"); //字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setItalic(true); //是否使用斜体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// font.setStrikeout(true); //是否使用划线
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置单元格类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFCellStyle cellStyle = workbook.createCellStyle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setFont(font);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setWrapText(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 添加单元格注释
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFPatriarch patr = sheet.createDrawingPatriarch();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 定义注释的大小和位置,详见文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置注释内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
comment.setAuthor("Xuys.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建单元格
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFCell cell = row.createCell((short) 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFRichTextString hssfString = new HSSFRichTextString("Hello World!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellValue(hssfString);//设置单元格内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellStyle(cellStyle);//设置单元格样式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//指定单元格格式:数值、公式或字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellComment(comment);//添加注释
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//格式化数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
row = sheet.createRow((short) 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell = row.createCell((short) 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellValue(11111.25);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle = workbook.createCellStyle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setDataFormat(format.getFormat("0.0"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellStyle(cellStyle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
row = sheet.createRow((short) 3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell = row.createCell((short) 3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellValue(9736279.073);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle = workbook.createCellStyle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellStyle(cellStyle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)0); //调整第一列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)1); //调整第二列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)2); //调整第三列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)3); //调整第四列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fileOut = new FileOutputStream("C:/3.xls");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
workbook.write(fileOut);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileOut.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
最后自动调整列宽的代码是在找了好久以后不经意间找着的,很惊喜!
如果编译器提示没有autoSizeColumn这个方法,那可能是你的poi版本太低的缘故,我用的是poi3.0版本。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFCell;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFFont;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFRow;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFSheet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFComment;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class PoiCreateExcelTest {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see <a href="http://poi.apache.org/hssf/quick-guide.html#NewWorkbook">For more</a>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建新的Excel 工作簿
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFWorkbook workbook = new HSSFWorkbook();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFSheet sheet = workbook.createSheet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//HSSFSheet sheet = workbook.createSheet("SheetName");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 用于格式化单元格的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFDataFormat format = workbook.createDataFormat();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建新行(row),并将单元格(cell)放入其中. 行号从0开始计算.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFRow row = sheet.createRow((short) 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFFont font = workbook.createFont();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setFontHeightInPoints((short) 20); //字体高度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setColor(HSSFFont.COLOR_RED); //字体颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setFontName("黑体"); //字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
font.setItalic(true); //是否使用斜体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// font.setStrikeout(true); //是否使用划线
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置单元格类型
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFCellStyle cellStyle = workbook.createCellStyle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setFont(font);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setWrapText(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 添加单元格注释
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFPatriarch patr = sheet.createDrawingPatriarch();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 定义注释的大小和位置,详见文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置注释内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
comment.setAuthor("Xuys.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 创建单元格
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFCell cell = row.createCell((short) 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HSSFRichTextString hssfString = new HSSFRichTextString("Hello World!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellValue(hssfString);//设置单元格内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellStyle(cellStyle);//设置单元格样式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//指定单元格格式:数值、公式或字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellComment(comment);//添加注释
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//格式化数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
row = sheet.createRow((short) 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell = row.createCell((short) 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellValue(11111.25);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle = workbook.createCellStyle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setDataFormat(format.getFormat("0.0"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellStyle(cellStyle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
row = sheet.createRow((short) 3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell = row.createCell((short) 3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellValue(9736279.073);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle = workbook.createCellStyle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.setCellStyle(cellStyle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)0); //调整第一列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)1); //调整第二列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)2); //调整第三列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sheet.autoSizeColumn((short)3); //调整第四列宽度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fileOut = new FileOutputStream("C:/3.xls");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
workbook.write(fileOut);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileOut.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
最后自动调整列宽的代码是在找了好久以后不经意间找着的,很惊喜!
如果编译器提示没有autoSizeColumn这个方法,那可能是你的poi版本太低的缘故,我用的是poi3.0版本。
相关文章推荐
- Swift学习之协议
- RxJava 学习笔记一
- PHP开发工具
- iOS真机调试时“process launch failed: timed out waiting for app to launch”问题
- 模拟按键和触摸
- 设计模式(2)--工厂模式
- tomcat安装与配置
- Ant打包时候报jre错误
- iOS-根据文本的长度确定label的宽高
- ubuntu_socket通信入门_sever.cpp/client.cpp_g++编译
- ASP网站项目编译
- 启动和关闭JBoss As 7.1.1脚本
- excel公式处理成绩表
- zTree控件加载权限信息的保存
- Unity学习笔记(4) --- Unity的界面排版: RectTransform
- H - Rescue hdu1242
- postgresql_fdw 跨数据库查询
- PyQt学习笔记01-Qt基本概念进阶篇
- 安全驾驶-双手开车(三十九)
- 表名在数据库中的存储大小写略解