首先,这个取决于你使用什么工具包,但是和你使用什么样的服务器无关。
然后,对于常用的两个工具包poi和jxl,它们的api各不相同,但是基本操作大同小异。下面我给个jxl的例子:public void createBook() {
WritableWorkbook book = null;
//定义表格的title
String[] titles = new String[5];
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
titles[columnIndex] = "标题" + columnIndex;
}
//行序号,标识当前是sheet的第几行。都是从0开始计数
int colIndex = 0; //定义每一个格子的宽度
final int COL_WIDTH = 20;
//我画一个简单的表格,格式如下:
// 标题1 标题2 标题3 标题4 标题5
// 格子11 格子12 格子13 格子14 格子15
// 格子21 格子22 格子23 格子24 格子25
try {
//指定这个excel文件将被写到哪里(可以是一个指向本地文件系统的os,也可以是来自HttpServletResponse的response.getOutputStream())
File file = new File("C:\\temp\\test.xls");
file.createNewFile();
OutputStream os = new FileOutputStream(file);
//创建excel文件
book = Workbook.createWorkbook(os);
//定义表格title和内容的字体以及格式
WritableFont dataFont = new WritableFont(WritableFont.ARIAL, 10);
WritableCellFormat dataFormat = new WritableCellFormat (dataFont);
WritableCellFormat dataFormatWithColor = new WritableCellFormat (dataFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont); //定义表格title的格式(居中显示)
titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
//定义表格内容的格式(水平靠左,竖直居中,自动换行)
dataFormat.setAlignment(jxl.format.Alignment.LEFT);
dataFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
dataFormat.setWrap(true); //定义表格内容的格式(水平靠左,竖直居中,自动换行)
dataFormatWithColor.setAlignment(jxl.format.Alignment.LEFT);
dataFormatWithColor.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
dataFormatWithColor.setWrap(true);
//建立工作表sheet,参数0代表第一页
WritableSheet sheet = book.createSheet("Sheet", 0); //给sheet添加表格的title:标题1 标题2 标题3 标题4 标题5
for(int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
Label titleLable = new Label(columnIndex, colIndex, titles[columnIndex], titleFormat);
sheet.setColumnView(columnIndex, COL_WIDTH);
sheet.addCell(titleLable );
}
//title是第一行,现在完成了title,所以需要换行了
colIndex++;
//开始添加表格数据了:格子11 格子12 格子13 格子14 格子15
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
Label cellLabel = new Label(columnIndex, colIndex, "格子" + colIndex + columnIndex);
cellLabel.setCellFormat(dataFormat);
sheet.addCell(cellLabel);
}
//下一行
colIndex++;
//开始添加表格数据了:格子11 格子12 格子13 格子14 格子15
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
Label cellLabel = new Label(columnIndex, colIndex, "格子" + colIndex + columnIndex);
cellLabel.setCellFormat(dataFormatWithColor);
sheet.addCell(cellLabel);
}
//将这个文件写入C:/temp/test.xls
book.write();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
然后,对于常用的两个工具包poi和jxl,它们的api各不相同,但是基本操作大同小异。下面我给个jxl的例子:public void createBook() {
WritableWorkbook book = null;
//定义表格的title
String[] titles = new String[5];
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
titles[columnIndex] = "标题" + columnIndex;
}
//行序号,标识当前是sheet的第几行。都是从0开始计数
int colIndex = 0; //定义每一个格子的宽度
final int COL_WIDTH = 20;
//我画一个简单的表格,格式如下:
// 标题1 标题2 标题3 标题4 标题5
// 格子11 格子12 格子13 格子14 格子15
// 格子21 格子22 格子23 格子24 格子25
try {
//指定这个excel文件将被写到哪里(可以是一个指向本地文件系统的os,也可以是来自HttpServletResponse的response.getOutputStream())
File file = new File("C:\\temp\\test.xls");
file.createNewFile();
OutputStream os = new FileOutputStream(file);
//创建excel文件
book = Workbook.createWorkbook(os);
//定义表格title和内容的字体以及格式
WritableFont dataFont = new WritableFont(WritableFont.ARIAL, 10);
WritableCellFormat dataFormat = new WritableCellFormat (dataFont);
WritableCellFormat dataFormatWithColor = new WritableCellFormat (dataFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont); //定义表格title的格式(居中显示)
titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
//定义表格内容的格式(水平靠左,竖直居中,自动换行)
dataFormat.setAlignment(jxl.format.Alignment.LEFT);
dataFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
dataFormat.setWrap(true); //定义表格内容的格式(水平靠左,竖直居中,自动换行)
dataFormatWithColor.setAlignment(jxl.format.Alignment.LEFT);
dataFormatWithColor.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
dataFormatWithColor.setWrap(true);
//建立工作表sheet,参数0代表第一页
WritableSheet sheet = book.createSheet("Sheet", 0); //给sheet添加表格的title:标题1 标题2 标题3 标题4 标题5
for(int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
Label titleLable = new Label(columnIndex, colIndex, titles[columnIndex], titleFormat);
sheet.setColumnView(columnIndex, COL_WIDTH);
sheet.addCell(titleLable );
}
//title是第一行,现在完成了title,所以需要换行了
colIndex++;
//开始添加表格数据了:格子11 格子12 格子13 格子14 格子15
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
Label cellLabel = new Label(columnIndex, colIndex, "格子" + colIndex + columnIndex);
cellLabel.setCellFormat(dataFormat);
sheet.addCell(cellLabel);
}
//下一行
colIndex++;
//开始添加表格数据了:格子11 格子12 格子13 格子14 格子15
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
Label cellLabel = new Label(columnIndex, colIndex, "格子" + colIndex + columnIndex);
cellLabel.setCellFormat(dataFormatWithColor);
sheet.addCell(cellLabel);
}
//将这个文件写入C:/temp/test.xls
book.write();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货