|
发表于 2014-4-29 11:31:42
|
显示全部楼层
就用你这个代码,路径中"/"换成"\"()
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sbr = new StringBuilder();
using (FileStream fs = File.OpenRead(@"e:\CO242.xls")) //打开myxls.xls文件
{
HSSFWorkbook wk = new HSSFWorkbook(fs); //把xls文件中的数据写入wk中
for (int i = 0; i < wk.NumberOfSheets; i++) //NumberOfSheets是myxls.xls中总共的表数
{
ISheet sheet = wk.GetSheetAt(i); //读取当前表数据
for (int j = 0; j <= sheet.LastRowNum; j++) //LastRowNum 是当前表的总行数
{
IRow row = sheet.GetRow(j); //读取当前行数据
if (row != null)
{
sbr.Append("-------------------------------------\r\n"); //读取行与行之间的提示界限
for (int k = 0; k <= row.LastCellNum; k++) //LastCellNum 是当前行的总列数
{
ICell cell = row.GetCell(k); //当前表格
if (cell != null)
{
sbr.Append(cell.ToString()); //获取表格中的数据并转换为字符串类型
}
}
}
}
}
}
sbr.ToString();
using (StreamWriter wr = new StreamWriter(new FileStream(@"c:\myText.txt", FileMode.Append))) //把读取xls文件的数据写入myText.txt文件中
{
wr.Write(sbr.ToString());
wr.Flush();
}
}
|
|