import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CreatExcelTest {
public static void creatExcel(){
try {
HSSFWorkbook wb = new HSSFWorkbook(); //建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet"); //建立新的sheet对象
//第0行
HSSFRow row = sheet.createRow((short)0);
//在sheet里创建一行,参数为行号(第一行,此处可想象成数组)
HSSFCell cell = row.createCell(0);
//在row里建立新cell(单元格),参数为列号(第一列)
cell.setCellValue(1) ;//设置cell的整数类型的值
row.createCell(1).setCellValue(1.2); //设置cell浮点类型的值
row.createCell(2).setCellValue("haha"); //设置cell字符类型的值
row.createCell(3).setCellValue(true); //设置cell布尔类型的值
HSSFCellStyle cellStyle = wb.createCellStyle(); //建立新的cell样式
HSSFDataFormat format= wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("yyyy年mm月d日 hh:mm:ss"));//这两行是日期格式的重点
//设置cell样式为定制的日期格式
HSSFCell dCell =row.createCell(4);
dCell.setCellValue(new Date()); //设置cell为日期类型的值
dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式
HSSFCell csCell =row.createCell(5);
// csCell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设置cell编码解决中文高位字节截断
csCell.setCellValue("这是中文的测试"); //设置中西文结合字符串
row.createCell(6).setCellType(HSSFCell.CELL_TYPE_ERROR);
//建立错误cell
//第1行
HSSFRow row1 = sheet.createRow(1);
row1.createCell(0).setCellValue(false);
row1.createCell(1).setCellValue(19907);
row1.createCell(2).setCellValue("中华民国a ");
//创建生成文件
String path = "D:\\Workspace\\TestPOI\\abcd.xls";
FileOutputStream fileOut = new FileOutputStream(new File(path));
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}