1.作业环境
复制代码 代码如下:
using Excel;
// from bill example
public void writeExcelAdvance(String outputFile)
{
string[,] myData =
{
{ "车牌号", "类型", "品 牌", "型 号", "颜 色", "附加费证号", "车架号" },
{ "浙KA3676", "危险品", "货车", "铁风SZG9220YY", "白", "1110708900", "022836" },
{ "浙KA4109", "危险品", "货车", "解放CA4110P1K2", "白", "223132", "010898" },
{ "浙KA0001A", "危险品", "货车", "南明LSY9190WS", "白", "1110205458", "0474636" },
{ "浙KA0493", "上普货", "货车", "解放LSY9190WS", "白", "1110255971", "0094327" },
{ "浙KA1045", "普货", "货车", "解放LSY9171WCD", "蓝", "1110391226", "0516003" },
{ "浙KA1313", "普货", "货车", "解放9190WCD", "蓝", "1110315027", "0538701" },
{ "浙KA1322", "普货", "货车", "解放LSY9190WS", "蓝", "24323332", "0538716" },
{ "浙KA1575", "普货", "货车", "解放LSY9181WCD", "蓝", "1110314149", "0113018" },
{ "浙KA1925", "普货", "货车", "解放LSY9220WCD", "蓝", "1110390626", "00268729" },
{ "浙KA2258", "普货", "货车", "解放LSY9220WSP", "蓝", "111048152", "00320" }
};
//引用Excel Application类别
Application myExcel = null;
//引用活页簿类别
Workbook myBook = null;
//引用工作表类别
Worksheet mySheet = null;
//引用Range类别
Range myRange = null;
//开启一个新的应用程式
myExcel = new Microsoft.Office.Interop.Excel.Application();
//打开一个已经存在的excel excel2003和2007版打开参数不一样,具体可以上网上去查:
//excelSql.Workbooks.Open(@"C:\08.xls"(已经存在的excel路径), Type.Missing, Type.Missing, Type.Missing, Type.Missing,
//Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing,Type.Missing);
//加入新的活页簿
myExcel.Workbooks.Add(true);
//停用警告讯息
myExcel.DisplayAlerts = false;
//让Excel文件可见
myExcel.Visible = true;
//引用第一个活页簿
myBook = myExcel.Workbooks[1];
//设定活页簿焦点
myBook.Activate();
//引用第一个工作表
mySheet = (Worksheet)myBook.Worksheets[1];
//命名工作表的名称为 "Array"
mySheet.Name = "Cells";
//设工作表焦点
mySheet.Activate();
int a = 0;
int UpBound1 = myData.GetUpperBound(0);
//二维阵列数上限
int UpBound2 = myData.GetUpperBound(1);
//二维阵列数上限
//写入报表名称
myExcel.Cells[1, 4] = "普通报表";
//以下的Select方法可省略,加速Excel运行,但VBA有些功能必须要用到Select方法。
//以下的Select方法可省略,加速Excel运行,但VBA有些功能必须要用到Select方法。
//逐行写入数据
for (int i = 0; i < UpBound1; i++)
{
for (int j = 0; j < UpBound2; j++)
{
//以单引号开头,表示该单元格为纯文字
a++;
//用offset写入阵列资料
myRange = mySheet.get_Range("A2", Type.Missing);
myRange.get_Offset(i, j).Select();
myRange.Value2 = "'" + myData[i, j];
//用Cells写入阵列资料
myRange.get_Range(myExcel.Cells[2 + i, 1 + j], myExcel.Cells[2 + i, 1 + j]).Select();
myExcel.Cells[2 + i, 1 + j] = "'" + myData[i, j];
}
}
//加入新的工作表在第1张工作表之后
myBook.Sheets.Add(Type.Missing, myBook.Worksheets[1], 1, Type.Missing);
//引用第2个工作表
mySheet = (Worksheet)myBook.Worksheets[2];
//命名工作表的名称为 "Array"
mySheet.Name = "Array";
//Console.WriteLine(mySheet.Name);
//写入报表名称
myExcel.Cells[1, 4] = "普通报表";
//设定范围
myRange = (Range)mySheet.get_Range(myExcel.Cells[2, 1], myExcel.Cells[UpBound1 + 1, UpBound2 + 1]);
myRange.Select();
//用阵列一次写入资料
myRange.Value2 = "'" + myData;
//设定储存路径
//string PathFile = Directory.GetCurrentDirectory() + @"\button4.xls";
//另存活页簿
myBook.SaveAs(outputFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//关闭活页簿
myBook.Close(false, Type.Missing, Type.Missing);
//关闭Excel
myExcel.Quit();
//释放Excel资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
mySheet = null;
myRange = null;
myExcel = null;
GC.Collect();
开发环境:vs2005 /vs2008
数据库:sql2005
复制代码 代码如下:
using Excel;
// from bill example
public void writeExcelAdvance(String outputFile)
{
string[,] myData =
{
{ "车牌号", "类型", "品 牌", "型 号", "颜 色", "附加费证号", "车架号" },
{ "浙KA3676", "危险品", "货车", "铁风SZG9220YY", "白", "1110708900", "022836" },
{ "浙KA4109", "危险品", "货车", "解放CA4110P1K2", "白", "223132", "010898" },
{ "浙KA0001A", "危险品", "货车", "南明LSY9190WS", "白", "1110205458", "0474636" },
{ "浙KA0493", "上普货", "货车", "解放LSY9190WS", "白", "1110255971", "0094327" },
{ "浙KA1045", "普货", "货车", "解放LSY9171WCD", "蓝", "1110391226", "0516003" },
{ "浙KA1313", "普货", "货车", "解放9190WCD", "蓝", "1110315027", "0538701" },
{ "浙KA1322", "普货", "货车", "解放LSY9190WS", "蓝", "24323332", "0538716" },
{ "浙KA1575", "普货", "货车", "解放LSY9181WCD", "蓝", "1110314149", "0113018" },
{ "浙KA1925", "普货", "货车", "解放LSY9220WCD", "蓝", "1110390626", "00268729" },
{ "浙KA2258", "普货", "货车", "解放LSY9220WSP", "蓝", "111048152", "00320" }
};
//引用Excel Application类别
Application myExcel = null;
//引用活页簿类别
Workbook myBook = null;
//引用工作表类别
Worksheet mySheet = null;
//引用Range类别
Range myRange = null;
//开启一个新的应用程式
myExcel = new Microsoft.Office.Interop.Excel.Application();
//打开一个已经存在的excel excel2003和2007版打开参数不一样,具体可以上网上去查:
//excelSql.Workbooks.Open(@"C:\08.xls"(已经存在的excel路径), Type.Missing, Type.Missing, Type.Missing, Type.Missing,
//Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing,Type.Missing);
//加入新的活页簿
myExcel.Workbooks.Add(true);
//停用警告讯息
myExcel.DisplayAlerts = false;
//让Excel文件可见
myExcel.Visible = true;
//引用第一个活页簿
myBook = myExcel.Workbooks[1];
//设定活页簿焦点
myBook.Activate();
//引用第一个工作表
mySheet = (Worksheet)myBook.Worksheets[1];
//命名工作表的名称为 "Array"
mySheet.Name = "Cells";
//设工作表焦点
mySheet.Activate();
int a = 0;
int UpBound1 = myData.GetUpperBound(0);
//二维阵列数上限
int UpBound2 = myData.GetUpperBound(1);
//二维阵列数上限
//写入报表名称
myExcel.Cells[1, 4] = "普通报表";
//以下的Select方法可省略,加速Excel运行,但VBA有些功能必须要用到Select方法。
//以下的Select方法可省略,加速Excel运行,但VBA有些功能必须要用到Select方法。
//逐行写入数据
for (int i = 0; i < UpBound1; i++)
{
for (int j = 0; j < UpBound2; j++)
{
//以单引号开头,表示该单元格为纯文字
a++;
//用offset写入阵列资料
myRange = mySheet.get_Range("A2", Type.Missing);
myRange.get_Offset(i, j).Select();
myRange.Value2 = "'" + myData[i, j];
//用Cells写入阵列资料
myRange.get_Range(myExcel.Cells[2 + i, 1 + j], myExcel.Cells[2 + i, 1 + j]).Select();
myExcel.Cells[2 + i, 1 + j] = "'" + myData[i, j];
}
}
//加入新的工作表在第1张工作表之后
myBook.Sheets.Add(Type.Missing, myBook.Worksheets[1], 1, Type.Missing);
//引用第2个工作表
mySheet = (Worksheet)myBook.Worksheets[2];
//命名工作表的名称为 "Array"
mySheet.Name = "Array";
//Console.WriteLine(mySheet.Name);
//写入报表名称
myExcel.Cells[1, 4] = "普通报表";
//设定范围
myRange = (Range)mySheet.get_Range(myExcel.Cells[2, 1], myExcel.Cells[UpBound1 + 1, UpBound2 + 1]);
myRange.Select();
//用阵列一次写入资料
myRange.Value2 = "'" + myData;
//设定储存路径
//string PathFile = Directory.GetCurrentDirectory() + @"\button4.xls";
//另存活页簿
myBook.SaveAs(outputFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//关闭活页簿
myBook.Close(false, Type.Missing, Type.Missing);
//关闭Excel
myExcel.Quit();
//释放Excel资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
mySheet = null;
myRange = null;
myExcel = null;
GC.Collect();
标签:
asp.net,excel
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“asp.net(C#)操作excel(上路篇)”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。