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();
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]