我现在要实现一个功能,Excel表上面的数据是往下排列的,现在我要读取全部的数据,把它变成一行一行的排列下来,该怎么做啊 现在是
郭枫
id : 12
name : ss
gb: ssdf 郭彪
id : 12
name : ss
gb: ssdf ........
现在要变成 id name password 12 ss dd
34 dd dd名字可以新增一个栏位,显示在一行数据里,这样该怎么做啊 我现在都没什么头绪,大家给我想想办法啊
郭枫
id : 12
name : ss
gb: ssdf 郭彪
id : 12
name : ss
gb: ssdf ........
现在要变成 id name password 12 ss dd
34 dd dd名字可以新增一个栏位,显示在一行数据里,这样该怎么做啊 我现在都没什么头绪,大家给我想想办法啊
或者直接使用excel里面的宏搞定连JAVA 都不用
慢慢写吧,谁有哪功夫按着你的需求给你写啊
import jxl.write.*;
import jxl.format.*;/** *//**
* 实现对Excel报表的操作.调用了开源框架jxl.
* @author shengyan
* @since
* @version 0.0.0.1
*/
public class ExcelHandle ...{ /** *//**
* 实现对excel报表的读取.
* @param sourcefile <code>String<code> parse path of file
*/
public void readExcel(String sourcefile) ...{
try ...{
// 构建Workbook对象, 只读Workbook对象
// 直接从本地文件创建Workbook
// 从输入流创建Workbook
InputStream is = new FileInputStream(sourcefile);
//获得workbook 工作薄
Workbook rwb = Workbook.getWorkbook(is);
//Workbook rwb = Workbook.getWorkbook(new File(sourcefile));
// 获取第一张Sheet 工作表
Sheet rs = rwb.getSheet(0);
// 获取第一行,第一列的值
Cell c00 = rs.getCell(0, 0);
String strc00 = c00.getContents();
// 获取第一行,第二列的值
Cell c10 = rs.getCell(1, 0);
String strc10 = c10.getContents();
// 获取第二行,第二列的值
Cell c11 = rs.getCell(1, 1);
String strc11 = c11.getContents();
// 获得cell具体类型值的方式
if (c00.getType() == CellType.LABEL) ...{
LabelCell labelc00 = (LabelCell) c00;
System.out.println(labelc00.getString());
}
System.out.println("Cell(0, 0)" + " value : " + strc00
+ "; type : " + c00.getType());
System.out.println("Cell(1, 0)" + " value : " + strc10
+ "; type : " + c10.getType());
System.out.println("Cell(1, 1)" + " value : " + strc11
+ "; type : " + c11.getType());
rwb.close();
is.close();
} catch (Exception e) ...{
e.printStackTrace();
} }
/** *//**
* 实现写Excel报表
* @param sourcefile <code>String<code> parse path of file
*/
public void writeExcel(String sourcefile) ...{
try ...{
File file = new File(sourcefile);
file.createNewFile();
OutputStream os = new FileOutputStream(file);
// 获得workbook
WritableWorkbook wwb = Workbook.createWorkbook(os);
//WritableWorkbook wwb = Workbook.createWorkbook(new File(sourcefile));
// 获得sheet 1.名字, 2.下标(从0开始)
WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
// 行高和列宽
//将第一行的高度设为200
ws.setRowView(0,200);
//将第一列的宽度设为30
ws.setColumnView(0,30);
//1. 添加的字体样式
/**//*
* 获得字体
* 第一个: TIMES是字体大小,他写的是18
* 第二个: BOLD是判断是否为斜体,选择true时为斜体
* 第三个: ARIAL
* 第四个: UnderlineStyle.NO_UNDERLINE 下划线
* 第五个: jxl.format.Colour.RED 字体颜色是红色的
*/
WritableFont wf = new WritableFont(WritableFont.TIMES, 18,
WritableFont.BOLD, true);
WritableCellFormat wcfF = new WritableCellFormat(wf);
/**//*
* 在Label()方法里面有三个参数
* 第一个是代表列数,
* 第二是代表行数,
* 第三个代表要写入的内容
* 第四个是可选项,是输入这个label里面的样式
*/
Label labelC = new Label(1, 0, "This is a Label cell", wcfF);
// 把内容写进sheet里面
ws.addCell(labelC);
//合并cell 单元格
Range rang = ws.mergeCells(0, 6, 2, 7);
WritableCellFormat ccf= new WritableCellFormat();
//把水平对齐方式指定为居中
ccf.setAlignment(jxl.format.Alignment.CENTRE);
//设置自动换行
ccf.setWrap(true);
Label labelz = new Label(0, 6, "merge label", ccf);
//Cell cell = rang.getTopLeft();
ws.addCell(labelz);
// 3.添加Number对象
// 3.1显示number对象数据的格式
// 带有formatting的Number对象
NumberFormat nf = new jxl.write.NumberFormat("#.##");
WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
/**//*
* Number()方法参数说明:
* 前两上表示输入的位置
* 第三个表示输入的内容
*/
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
// 4. 添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB); // 5.添加DateTime对象
/**//*
* DateTime()方法的参数说明
* 前两个表示输入的位置
* 第三个表示输入的当前时间
*/
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
// 6.添加带有formatting的DateFormat对象
jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
ws.addCell(labelDTF); // 7.添加带有字体颜色Formatting的对象
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc); // 8.设置单元格样式
wcfFC.setBackground(jxl.format.Colour.RED); //设置单元格的颜色为红色
ws.addCell(new jxl.write.Label(6,0,"i love china",wcfFC)); // 9.添加图片对象,jxl只支持png格式图片*pV)-s
File image = new File("c:\2.png");
WritableImage wimage = new WritableImage(0,1,2,2,image);
ws.addImage(wimage); // 写入excel
wwb.write();
wwb.close();
os.close();
} catch (Exception e) ...{
e.printStackTrace();
}
}
public static void main(String[] args) ...{
ExcelHandle re = new ExcelHandle();
String sourcefile = "c:/Book1.xls";
String tagfile = "c:/Book1.xls";
re.writeExcel(sourcefile);
re.readExcel(tagfile);
}}