
XlsMain java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
/
@author Hongten</br>
参考地址:>
举个例子吧
public class CreateSimpleExcelToDisk
{
/
@功能:手工构建一个简单格式的Excel
/
public static List<Tsxx> getStudent(String[] str) throws Exception
{
List listts = new ArrayList();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Connection conn = DBUtilgetCon();
String sqlback="";
for(int i=0;i<strlength-1;i++){
sqlback=sqlback+"\"id\"='"+str[i]+"'or";
}
sqlback=sqlback+"\"id\"='"+str[strlength-1]+"'";
String sql1 = "select from 表名 where"+sqlback;
Systemoutprintln(sql1 );
ResultSet rs = DBUtilgetResult(conn, sql1);
String outStr = "";
int a=1;
try {
while(rsnext()){
String objid = rsgetString(1);
String tsname= rsgetString("tsname");
String tstel= rsgetString("tstel");
String tscpname= rsgetString("tscpname");
String tsads=rsgetString("tsads");
String tsqus=rsgetString("tsqus");
String tsno=rsgetString("tsno");
String tsdate=rsgetString("tsdate");
Tsxx user = new Tsxx(a,tsname,tstel,tscpname,tsads,tsqus,tsno,dfparse(tsdate));
listtsadd(user);
a++;
}
} catch (SQLException e) {
eprintStackTrace();
}finally {
try {
if (rs != null) {
rsclose();
}
if (conn != null) {
connclose();
}
} catch (Exception e) {
}}
return listts;
}
public static void main(String[] args) throws Exception
{
// // 第一步,创建一个webbook,对应一个Excel文件
// HSSFWorkbook wb = new HSSFWorkbook();
// // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
// HSSFSheet sheet = wbcreateSheet("学生表一");
// // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
// HSSFRow row = sheetcreateRow((int) 0);
// // 第四步,创建单元格,并设置值表头 设置表头居中
// HSSFCellStyle style = wbcreateCellStyle();
// stylesetAlignment(HSSFCellStyleALIGN_CENTER); // 创建一个居中格式
//
// HSSFCell cell = rowcreateCell((short) 0);
// cellsetCellValue("学号");
// cellsetCellStyle(style);
// cell = rowcreateCell((short) 1);
// cellsetCellValue("姓名");
// cellsetCellStyle(style);
// cell = rowcreateCell((short) 2);
// cellsetCellValue("年龄");
// cellsetCellStyle(style);
// cell = rowcreateCell((short) 3);
// cellsetCellValue("生日");
// cellsetCellStyle(style);
//
// // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
// List list = CreateSimpleExcelToDiskgetStudent();
//
// for (int i = 0; i < listsize(); i++)
// {
// row = sheetcreateRow((int) i + 1);
// Tsxx stu = (Tsxx) listget(i);
// // 第四步,创建单元格,并设置值
// rowcreateCell((short) 0)setCellValue((double) stugetId());
// rowcreateCell((short) 1)setCellValue(stugetName());
// rowcreateCell((short) 2)setCellValue((double) stugetAge());
// cell = rowcreateCell((short) 3);
// cellsetCellValue(new SimpleDateFormat("yyyy-mm-dd")format(stugetBirth()));
// }
// // 第六步,将文件存到指定位置
// try
// {
// FileOutputStream fout = new FileOutputStream("E:/studentsxls");
// wbwrite(fout);
// foutclose();
// }
// catch (Exception e)
// {
// eprintStackTrace();
// }
}
按列读取的方法:
String pathname = "E:\\files\\titlexlsx";
File file = new File(pathname);
InputStream in = new FileInputStream(file);
//得到整个excel对象
XSSFWorkbook excel = new XSSFWorkbook(in);
//获取整个excel有多少个sheet
int sheets = excelgetNumberOfSheets();
//便利第一个sheet
Map<String,String> colMap = new HashMap<String, String>();
for(int i = 0 ; i < sheets ; i++ ){
XSSFSheet sheet = excelgetSheetAt(i);
if(sheet == null){
continue;
}
int mergedRegions = sheetgetNumMergedRegions();
XSSFRow row2 = sheetgetRow(0);
Map<Integer,String> category = new HashMap<Integer, String>();
for(int j = 0 ; j < mergedRegions; j++ ){
CellRangeAddress rangeAddress = sheetgetMergedRegion(j);
int firstRow = rangeAddressgetFirstColumn();
int lastRow = rangeAddressgetLastColumn();
categoryput(rangeAddressgetFirstColumn(), rangeAddressgetLastColumn()+"-"+row2getCell(firstRow)toString());
}
//便利每一行
for( int rowNum = 1 ; rowNum <= sheetgetLastRowNum() ; rowNum++ ){
Systemoutprintln();
XSSFRow row = sheetgetRow(rowNum);
if(row == null){
continue;
}
short lastCellNum = rowgetLastCellNum();
String cate = "";
Integer maxIndex = 0;
for( int col = rowgetFirstCellNum() ; col < lastCellNum ; col++ ){
XSSFCell cell = rowgetCell(col);
if(cell == null ){
continue;
}
if(""equals(celltoString())){
continue;
}
int columnIndex = cellgetColumnIndex();
String string = categoryget(columnIndex);
if(string != null && !stringequals("")){
String[] split = stringsplit("-");
cate = split[1];
maxIndex = IntegerparseInt(split[0]);
Systemoutprintln(cate+"<-->"+celltoString());
}else {
//如果当前便利的列编号小于等于合并单元格的结束,说明分类还是上面的分类名称
if(columnIndex<=maxIndex){
Systemoutprintln(cate+"<-->"+celltoString());
}else {
Systemoutprintln("分类未知"+"<-->"+celltoString());
}
}
}
}
}
}
以上就是关于用javaPOI包的方法获取excel中的行列号全部的内容,包括:用javaPOI包的方法获取excel中的行列号、java poi Excel2007里的SXSSFWorkbook怎样读取文件 SXSSFWorkbook只能写吗,那怎样获得要写的内容、JAVA,POI导出EXCEL表,表中所有数据都是从后台直接获取,求指导,越详细越好等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)