我要读取excel文件,在android上面,网上有说用poi,和jxl,我开始用的jxl,但是我不知道怎么合并单元格,

2024-12-21 12:28:01
推荐回答(4个)
回答1:

导入jxl.jar包
/**
* 功能:合并单元格
* @param wsheet 要操作的表
* @param firstRow int 起始行
* @param lastRow int 结束行
* @param firstColumn int 起始列
* @param lastColumn int 结束列
*/
public static Object mergeCells(WritableSheet sheet, int firstColumn,
int firstRow, int lastColumn, int lastRow){

WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));
wsheet = Ab.createSheet(title, workbook.getNumberOfSheets());// 第一张工作表
wsheet.mergeCells(firstColumn, firstRow, lastColumn, lastRow);
}

回答2:

public class ExcelDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.tv);
String path = "mnt/sdcard/test.xls";
writeExcel(path);
// textView.setText(readExcel(path,2,1)+readExcel(path,1,2));
}

public void writeExcel(String fileName)
{
WritableWorkbook wwb = null;
try
{
//创建一个可写入的工作薄(Workbook)对象
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e)
{
e.printStackTrace();
}
if (wwb != null)
{
// 第一个参数是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet("sheet1", 0);
// 在指定单元格插入数据
Label lbl1 = new Label(1, 0, "用户名");
Label bll2 = new Label(2, 0, "手机号");
Label bll3 = new Label(3, 0, "住址");
Label bll4 = new Label(4, 0, "生日");
Label bll5 = new Label(5, 0, "Email");
try
{
ws.addCell(lbl1);
ws.addCell(bll2);
ws.addCell(bll3);
ws.addCell(bll4);
ws.addCell(bll5);
} catch (RowsExceededException e1)
{
e1.printStackTrace();
} catch (WriteException e1)
{
e1.printStackTrace();
}
try
{
// 从内存中写入文件中
wwb.write();
wwb.close();
} catch (IOException e)
{
e.printStackTrace();
} catch (WriteException e)
{
e.printStackTrace();
}
}
}

public String readExcel(String path, int x, int y)
{
String content = "";
try
{
Workbook book = Workbook.getWorkbook(new File(path));
Sheet sheet = book.getSheet(0);
//得到x行y列所在单元格的内容
String cellStr = sheet.getRow(x)[y].getContents();
content = cellStr;

} catch (BiffException e)
{
content = "";
e.printStackTrace();
} catch (IOException e)
{
content = "";
e.printStackTrace();
}
return content;
}

}

回答3:

推荐POI,非常强大。

回答4:

你读取excel文件 为什么要合并单元格呢?