关于excel怎么操作比较方面,我这里要实现一个导入功能,会把我的数据导给用户。主要是excel模板(我是swing实现的,所以希望提供java代码查考)。这个模板主要就是合并比较麻烦,另外里面的有复选框等等这些,还有一个关键是中间有些内容他的行数是不固定的,如片参考:http://zhidao.baidu.com/question/319120573.html
解决方案 »
- tomcat的unable to compile class for jsp的问题,简单的页面能编译,连接数据库的就不能了。
- struts2.3.1.2中使用<sx:tree>标签生成动态树形结构的问题
- 如何使用java将cgm转换成pdf文件?求大侠相助
- java发展问题
- JSP生成word
- 初学者示救!!!
- 我现在刚刚学jsp,看书上说机器的配置,为什么要j2se的jdk而不是j2ee的jdk啊?
- 蔡鸟问题:如何分析一个xml文件,从中取出数据项(元素)?万封感谢!
- petstore的问题,关于屏幕切换
- axis安装一问,搞不定了
- 在线等。
- Calendar 类型的数据在jsp中显示的问题
封装了一个写数据的
一个调用格式撒的 原理就是基于模板修改数据// copy附件
FileUtil.copyFile(from, to);
// 准备修改
FileInputStream fis=new FileInputStream(to);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);写数据的类import java.util.List;import org.apache.poi.hssf.usermodel.HSSFAnchor;
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;
public class AffixExcel {
/*
* 写一行分离出来
*/
public void setRegionRow(HSSFSheet sheet,int row,List<KpiRegionValBean> urkl,String kpiname,HSSFCellStyle cs,HSSFCellStyle cs1){
if (row == 2) {
HSSFRow row1 = sheet.getRow(1);
if(row1==null)
row1 = sheet.createRow(1);
for (int ni = 0; ni < urkl.size(); ni++) {
HSSFCell c1 = row1.getCell(ni + 1);
if (c1 == null)
c1 = row1.createCell(ni + 1);
c1.setCellStyle(cs1);
c1.setCellType(HSSFCell.CELL_TYPE_STRING);
c1.setCellValue(urkl.get(ni).getRegion_name());
}
} HSSFRow row2 = sheet.createRow(row);
if(row2==null)
row2 = sheet.createRow(row);
HSSFCell c = row2.getCell(0);
if (c == null)
c = row2.createCell(0);
c.setCellStyle(cs1);
c.setCellType(HSSFCell.CELL_TYPE_STRING);
c.setCellValue(kpiname);
for (int mi = 0; mi < urkl.size(); mi++) {
c = row2.getCell(mi + 1);
if (c == null)
c = row2.createCell(mi + 1);
c.setCellStyle(cs);
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(Double.valueOf(urkl.get(mi).getVal()));
}
}
/*
* 历史数据写入
*/
public void setDateRow(HSSFSheet sheet,int rowi,List<kpiDateValBean> urkd,String kpiname,HSSFCellStyle cs,HSSFCellStyle cs1){
if (rowi == 2) {
HSSFRow row = sheet.getRow(1);
if(row==null)
row = sheet.createRow(1);
for (int ni = 0; ni < urkd.size(); ni++) {
HSSFCell c = row.getCell(ni + 1);
if (c == null)
c = row.createCell(ni + 1);
c.setCellStyle(cs1);
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(urkd.get(ni).getDate_key()
.substring(
urkd.get(ni).getDate_key()
.length() - 2));
}
}
HSSFRow row2 = sheet.createRow(rowi);
if(row2==null)
row2 = sheet.createRow(rowi);
HSSFCell c1 = row2.getCell(0);
if (c1 == null)
c1 = row2.createCell(0);
c1.setCellStyle(cs1);
c1.setCellType(HSSFCell.CELL_TYPE_STRING);
c1.setCellValue(kpiname);
for (int mi = 0; mi < urkd.size(); mi++) {
c1 = row2.getCell(mi + 1);
if (c1 == null)
c1 = row2.createCell(mi + 1);
c1.setCellStyle(cs);
c1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c1.setCellValue(Double.valueOf(urkd.get(mi).getVal()));
}
}
/*
* 写第一行的时间
*/
public void setTime(HSSFSheet sheet,String time,HSSFCellStyle cs){
HSSFRow row = sheet.getRow(0);
if(row==null)
row = sheet.createRow(0);
HSSFCell cell = row.getCell(0);
if (cell == null)
cell = row.createCell(0);
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell.setCellStyle(cs);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(time);
}
/*
* 写第一行的名称
*/
public void setTitle(HSSFSheet sheet,String Title,HSSFCellStyle cs){
HSSFRow row = sheet.getRow(0);
if(row==null)
row = sheet.createRow(0);
HSSFCell cell = row.getCell(0);
if (cell == null)
cell = row.createCell(0);
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell.setCellStyle(cs);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(Title);
}
}使用的lib poi.jar 别的你自己找找吧
哥们,进去后没有找到下载的资源,能否传我一份呢,邮箱505777612qq.com,谢谢
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class PoiExcel2 {
public static List<String[]> read(String file, boolean flag) throws Exception {
Workbook wb = null;
File f = new File(file);
FileInputStream is = new FileInputStream(f);
if (flag) {// 2003
wb = new HSSFWorkbook(is);
} else {// 2007
wb = new XSSFWorkbook(is);
}
return read(wb);
} public static List<String[]> read(Workbook wb) throws Exception {
List<String[]> list =new ArrayList<String[]>();
try {
for (int k = 0; k < wb.getNumberOfSheets(); k++) { //页
// sheet
Sheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) { //行
String str = new String();
// 定义 row
Row row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) { //列
Cell cell = row.getCell(c);
if (cell != null) {
String value = null; switch (cell.getCellType()) { case Cell.CELL_TYPE_FORMULA:
value = cell.getCellFormula();
break; case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
value =cell.getDateCellValue().toString();
} else {
int number = (int) cell.getNumericCellValue();
value = number + "";
}
break;
case Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue() + "";
cell.getDateCellValue(); break;
default:
}
System.out.println(value);
// 第三行才开始添加数据
if (r > 1 || r == 1) {
//如果是最后一列,不加“ ,”
if(c!=cells){
str += value + ",";
}else{
str += value;
}
}
} //判断cell是否为空结束
} //循环列结束
//将数组存入List集合中,从第二列开始加到数组
if(r>1 || r==1){
String[] strCell = str.split(",");
list.add(strCell);
}
} //判断行是否为空结束
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
} public static void main(String[] args) throws Exception {
List<String[]> list=read("f:\\历史上的今天-资料.xlsx", false);
System.out.println("list大小:"+list.size());
/* for(int i=0;i<list.size();i++){
String[] c=list.get(i);
for(int k=0;k<c.length;k++){
System.out.println(c[k]);
}
}*/
}}
使用的是poi读取Excel数据到数据
下划线
HSSFCellStyle style = book.createCellStyle();
Font font = book.createFont();
font.setUnderline(Font.U_SINGLE);
style.setFont(font);
cell.setCellStyle(style);
combobox没找到。
google poi combobox
http://download.csdn.net/source/3561738源码
合并方法
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 4));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 5, 18));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 19, 34));