c#中如何打开一个已经存在的excel表格
excel表格是c#上一个动作建好的,然后close了。经过一些数据处理后,需要把数据输入到已经建好的excel中,需要在打开这个excel,并且把数据写入excel,进...
excel表格是c#上一个动作建好的,然后close了。经过一些数据处理后,需要把数据输入到已经建好的excel中,需要在打开这个excel,并且把数据写入excel,进行简单的处理
展开
3个回答
展开全部
C#中用Automation操作EXCel是非常低效率的,你可以去网上搜搜“NPOI”的资料,非常简单,连Excel都不用装。NPOI使用也很简单。
private void button3_Click(object sender, EventArgs e)
{
using (FileStream stream = new FileStream(@"c:\客户1.xls", FileMode.Open, FileAccess.Read))
{
HSSFWorkbook workbook = new HSSFWorkbook(stream);
HSSFSheet sheet = workbook.GetSheetAt(0);
for (int i = 0; i <= sheet.LastRowNum; i++)//LastRowNum最后一行的行号
{
HSSFRow row = sheet.GetRow(i);
string 客户姓名 = row.GetCell(0).StringCellValue;
string 客户电话 = row.GetCell(1).StringCellValue;
MessageBox.Show(string.Format("客户姓名{0},客户电话{1}",客户姓名,客户电话));
}
}
}
private void button4_Click(object sender, EventArgs e)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
HSSFRow row = sheet.CreateRow(0);
HSSFCell cell1 = row.CreateCell(0,HSSFCell.CELL_TYPE_STRING);
cell1.SetCellValue("hell0");
//row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue("Hello");
row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(3.14);
using (FileStream stream = new FileStream(@"c:\1.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
workbook.Write(stream);
}
MessageBox.Show("导出成功!");
}
private void button3_Click(object sender, EventArgs e)
{
using (FileStream stream = new FileStream(@"c:\客户1.xls", FileMode.Open, FileAccess.Read))
{
HSSFWorkbook workbook = new HSSFWorkbook(stream);
HSSFSheet sheet = workbook.GetSheetAt(0);
for (int i = 0; i <= sheet.LastRowNum; i++)//LastRowNum最后一行的行号
{
HSSFRow row = sheet.GetRow(i);
string 客户姓名 = row.GetCell(0).StringCellValue;
string 客户电话 = row.GetCell(1).StringCellValue;
MessageBox.Show(string.Format("客户姓名{0},客户电话{1}",客户姓名,客户电话));
}
}
}
private void button4_Click(object sender, EventArgs e)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
HSSFRow row = sheet.CreateRow(0);
HSSFCell cell1 = row.CreateCell(0,HSSFCell.CELL_TYPE_STRING);
cell1.SetCellValue("hell0");
//row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue("Hello");
row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC).SetCellValue(3.14);
using (FileStream stream = new FileStream(@"c:\1.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
workbook.Write(stream);
}
MessageBox.Show("导出成功!");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询