全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
6636 11
2012-06-03
       还在纠结用如何用R读取Excel的同学有福了。昨天逛CRAN的时候发现了一个xlsx包,它给出的介绍是可以读取、写入Excel 2007/2003文件并支持格式的设置。简单地来说,将Excel读取为数据框,以及将数据框写入为Excel文件都不是问题,而更加强大的是它能处理 Excel中的格式,比如合并单元格,设置列的宽度,设置字体和颜色等等。
如果只需要基本的读取/写入操作,那么其中的read.xlsx()write.xlsx()应该就能满足大部分的需求了,其用法也很简单,看看帮助文档就了解了。此外,还有两个相应的函数read.xlsx2()write.xlsx2(),按作者的话说,这两个函数使用了不同的实现方式,效率上会更高一些。
    除了基本的读写操作之外,如之前所说,xlsx包还能进行格式方面的设置。下面是一个简单的例子,说明了如何创建工作簿和工作表,如何操作单元格等。感兴趣的朋友不妨运行一下下面的例子,看看最终的效果。
ind = read.table(url("http://yixuan.cos.name/cn/wp-content/uploads/2012/01/ind.txt"),
                 sep = "\t");

library(xlsx);
# Create a new workbook
wb = createWorkbook();
# Create a new sheet with a name
sheet1 = createSheet(wb, "第一页");
# Set the zoom ratio when you open the Excel file
setZoom(sheet1, 50, 100);
# Set the width of columns
setColumnWidth(sheet1, 1:100, 2.8);

# Create rows
rows = createRow(sheet1, 1:40);
# Create cells for each row
cells = createCell(rows, 1:73);
# Merge the first row into one cell
addMergedRegion(sheet1, 1, 1, 1, 73);
# Create the style for title cell
title_cell_style = CellStyle(wb,
    alignment = Alignment(horizontal = "ALIGN_CENTER"),
    font = Font(wb, "blue", 50, isBold = TRUE));
# Create the style for black cells
black_cell_style = CellStyle(wb,
    border = Border(),
    fill = Fill(foregroundColor= "black"));
# Get the first row
first_row = getRows(sheet1, 1);
# Get the title cell from first row
title_cell = getCells(first_row, 1)[[1]];
# Set the value of the title cell
setCellValue(title_cell, "Read/Write Excel!");
# Set the style of the title cell
setCellStyle(title_cell, title_cell_style);
# Set the style of black cells
tmp = mapply(function(x, y) setCellStyle(cells[[x, y]], black_cell_style),
       ind[, 1] 3, ind[, 2] 5);
# Save the workbook into a file
saveWorkbook(wb, "test.xlsx");
总的来说,xlsx包是我目前见过的功能最全的操作Excel的R包,它只依赖于Java环境和rJava、xlsxjars两个包,在多种平台下 都能运行,局限是写操作只支持Excel 2007格式(*.xlsx),对于机器上只有MS Office 2003的人来说可能会有些不便。(LibreOffice和OpenOffice.org都可以打开Excel 2007文件)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-6-3 08:55:19
谢谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-6-3 09:17:21
jiangqing001 发表于 2012-6-3 08:55
谢谢
好东西,大家享!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-6-3 12:38:07
谢谢分享
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-6-3 13:46:54
thanks
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-6-4 01:55:06
谢谢分享。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群