首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VBA >

C#操作Excel表格解决方法

2012-04-01 
C#操作Excel表格请问怎么用C#操作Excel表格,主要是要写的数据相当大,所以有以下几个要求:1.写的时候不需要

C#操作Excel表格
请问怎么用C#操作Excel表格,主要是要写的数据相当大,所以有以下几个要求:
  1.写的时候不需要打开excel表格,
  2.需要具体名字的不同sheet,
  3.需要具体操作各个sheet下面的每一个cell
  4.最好可以直接用循环之类的语句操作cells

先谢谢各位了。

[解决办法]
vsto,设置excel的application的visible=false即可使得excel不可见。
循环sheets,循环cells。
[解决办法]
是这个吗?
string dt = "导出Excel";
Excel.Pictures pics = sheet.Pictures(missing) as Excel.Pictures;
(sheet.Cells[1, 3] as Excel.Range).Value = dt;
Excel.Range cell;//编码,名称,规格,颜色,仓库,零售实时库存
(sheet.Cells[1, 1] as Excel.Range).RowHeight = 20;
AddHeader(sheet, 1, 12, "xyz");
AddHeader(sheet, 2, 12, "编码");
AddHeader(sheet, 3, 30, "名称");

AddHeader(sheet, 4, 12, "规格");
AddHeader(sheet, 5, 12, "颜色");
AddHeader(sheet, 6, 12, "仓库");
AddHeader(sheet, 7, 12, "零售实时库存");


 if (products != null)
{
for (int i = 0; i < products.Count; i++)
{
int rowIndex = i + 3;
cell = sheet.Cells[rowIndex, 1] as Excel.Range;
cell.RowHeight = 20;
SetCellStyle(cell, true);
SetCellText(sheet, rowIndex, 1, "" + products[i].CHP_NAME);
SetCellText(sheet, rowIndex, 2, "" + products[i].PRD_CODE);
SetCellText(sheet, rowIndex, 3, "" + products[i].PRD_LOCALNAME);

SetCellText(sheet, rowIndex, 4, "" + products[i].PRD_SPEC);
SetCellText(sheet, rowIndex, 5, "" + products[i].PRD_COLOR);
SetCellText(sheet, rowIndex, 6, "" + products[i].TAG_DESC);
SetCellText(sheet, rowIndex, 7, "" + products[i].HST_QTY);

}

}

workbook.SaveAs(path, missing, missing, missing, missing,
missing, Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing,
missing, missing);

app.Quit();

byte[] buffer;
using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.Open))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
}

System.IO.File.Delete(path);
return File(buffer, "application/vnd.ms-excel", "信息导出.xls");
}

热点排行