如何用aspasp excel 读取xlsxExcel文件

更多精彩视频
姓名昵称:
&分袂与不舍老是充斥着七月的高校校园,将还没诉说的遗憾留给一…
减肥和饮食从来就是势不两立的两方,对于那些想瘦的人来说,必须从两者中选…
灰色T恤+蓝色仔裤,卷起裤脚,随意休闲!
印花让白T恤生动起来! …
&&地铁呼啸、站台等待,便捷穿梭于繁华城市的地下生…
24小时点击|
&&&&&常常跟自己v, 我是真的茬笑, 可心什麽s疼的那麽底。文 …[]
QQ个性签名/唯美网名
现下流行非主流,特别是90后的人。今天在查询搜索词……
日晚,李玉刚《镜花水月》全球巡演的第11场演出在……页面导航:
→ 正文内容 asp.net读取excel文件
asp.net读取excel文件的三种方法示例
这篇文章主要介绍了asp.net读取excel文件的三种方法示例,包括采用OleDB读取Excel文件、引用的com组件读取Excel文件、用文件流读取,需要的朋友可以参考下
方法一:采用OleDB读取Excel文件
把Excel文件当做一个数据源来进行数据的读取操作,实例如下: 代码如下:public DataSet ExcelToDS(string Path)&& {&& string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";&& OleDbConnection conn = new OleDbConnection(strConn);&& conn.Open();& string strExcel = "";&& OleDbDataAdapter myCommand =&& DataSet ds =&& strExcel="select * from [sheet1$]";&& myCommand = new OleDbDataAdapter(strExcel, strConn);&& ds = new DataSet();&& myCommand.Fill(ds,"table1");&& && }
对于Excel中的表即sheet([sheet1$])如果不是固定的可以使用下面的方法得到
代码如下:string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null); string tableName=schemaTable.Rows[0][2].ToString().Trim();
另外:也可进行写入Excel文件,实例如下:
代码如下:public void DSToExcel(string Path,DataSet oldds)&& {&& //先得到汇总Excel的DataSet 主要目的是获得Excel在DataSet中的结构&& string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+path1+";Extended Properties=Excel 8.0" ;&& OleDbConnection myConn = new OleDbConnection(strCon) ;&& string strCom="select * from [Sheet1$]";&& myConn.Open ( ) ;&& OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom, myConn ) ;&& system.Data.OleDb.OleDbCommandBuilder builder=new OleDbCommandBuilder(myCommand);&& //QuotePrefix和QuoteSuffix主要是对builder生成InsertComment命令时使用。&& builder.QuotePrefix="["; //获取insert语句中保留字符(起始位置)&& builder.QuoteSuffix="]"; //获取insert语句中保留字符(结束位置)&& DataSet newds=new DataSet();&& myCommand.Fill(newds ,"Table1") ;&& for(int i=0;i&oldds.Tables[0].Rows.Ci++)&& {&& //在这里不能使用ImportRow方法将一行导入到news中,&& //因为ImportRow将保留原来DataRow的所有设置(DataRowState状态不变)。&& //在使用ImportRow后newds内有值,但不能更新到Excel中因为所有导入行的DataRowState!=Added&& DataRow nrow=aDataSet.Tables["Table1"].NewRow();&& for(int j=0;j&newds.Tables[0].Columns.Cj++)&& {&& nrow[j]=oldds.Tables[0].Rows[i][j];&& }&& newds.Tables["Table1"].Rows.Add(nrow);&& }&& myCommand.Update(newds,"Table1");&& myConn.Close();&& }
方法二:引用的com组件:Microsoft.Office.Interop.Excel.dll读取Excel文件
首先是Excel.dll的获取,将Office安装目录下的Excel.exe文件Copy到DotNet的bin目录下,cmd到该目录下,运行 TlbImp EXCEL.EXE Excel.dll 得到Dll文件。
在项目中添加引用该dll文件
代码如下://读取EXCEL的方法&&& (用范围区域读取数据)&& private void OpenExcel(string strFileName)&& {&& object missing = System.Reflection.Missing.V&& Application excel = new Application();//lauch excel application&& &&& if (excel == null)&& {&& &&& Response.Write("&script&alert('Can't access excel')&/script&");&& }&& &&& else&& {&& excel.Visible =&& excel.UserControl =&& // 以只读的形式打开EXCEL文件&& Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,&& missing, missing, missing, true, missing, missing, missing, missing, missing);&& //取得第一个工作薄Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);//取得总记录行数(包括标题列)int rowsint = ws.UsedRange.Cells.Rows.C //得到行数//int columnsint = mySheet.UsedRange.Cells.Columns.C//得到列数//取得数据范围区域(不包括标题列)Range rng1 = ws.Cells.get_Range("B2", "B" + rowsint);//itemRange rng2 = ws.Cells.get_Range("K2", "K" + rowsint);& //Customerobject[,] arryItem= (object[,])rng1.Value2;//get range's valueobject[,] arryCus = (object[,])rng2.Value2; //将新值赋给一个数组string[,] arry = new string[rowsint-1, 2];for (int i = 1; i &= rowsint-1; i++){//Item_Code列arry[i - 1, 0] =arryItem[i, 1].ToString();//Customer_Name列arry[i - 1, 1] = arryCus[i, 1].ToString();}Response.Write(arry[0, 0] + "/" + arry[0, 1] + "#" + arry[rowsint - 2, 0] + "/" + arry[rowsint - 2, 1]);}&excel.Quit();excel =Process[] procs = Process.GetProcessesByName("excel");foreach (Process pro in procs){pro.Kill();//没有更好的方法,只有杀掉进程}GC.Collect();}
方法三:将Excel文件转化成CSV(逗号分隔)的文件,用文件流读取(等价就是读取一个txt文本文件)。
先引用命名空间:
代码如下:using System.T和using System.IO;&& FileStream fs = new FileStream("d:\\Customer.csv", FileMode.Open, FileAccess.Read, FileShare.None);&& StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));&& string str = "";&& string s = Console.ReadLine();&& while (str != null)&& {&&&&&& &&& str = sr.ReadLine();&& &&& string[] xu = new String[2];&& &&& xu = str.Split(',');&& &&& string ser = xu[0];&& &&& string dse = xu[1];&&&&&&&&&&&&&&&&& &&& if (ser == s)&& &&& {&&& &&&&&&& Console.WriteLine(dse);&& &&& }&& }&&&&& sr.Close();
另外也可以将数据库数据导入到一个txt文件,实例如下:
代码如下://txt文件名&& string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + "PO014" + ".txt";&& OleDbConnection con = new OleDbConnection(conStr);&& con.Open();&& string sql = "select&& ITEM,REQD_DATE,QTY,PUR_FLG,PO_NUM from TSD_PO014";&&&&&&&&&& /OleDbCommand mycom = new OleDbCommand("select * from TSD_PO014", mycon);&& //OleDbDataReader myreader = mycom.ExecuteReader();&& //也可以用Reader读取数据&& DataSet ds = new DataSet();&& OleDbDataAdapter oda = new OleDbDataAdapter(sql, con);&& oda.Fill(ds, "PO014");&& DataTable dt = ds.Tables[0];&& FileStream fs = new FileStream(Server.MapPath("download/" + fn), FileMode.Create, FileAccess.ReadWrite);&& StreamWriter strmWriter = new StreamWriter(fs);&&& //存入到文本文件中&& //把标题写入.txt文件中&& //for (int i = 0; i &dt.Columns.Ci++)&& //{&& //&&&& strmWriter.Write(dt.Columns[i].ColumnName + "&& ");&& //}&& foreach (DataRow dr in dt.Rows)&& {&& string str0, str1, str2, str3;&& string str = "|";& //数据用"|"分隔开&& str0=dr[0].ToString();str1=dr[1].ToString();str2=dr[2].ToString();str3=dr[3].ToString();str4=dr[4].ToString().Trim();strmWriter.Write(str0);strmWriter.Write(str);strmWriter.Write(str1);strmWriter.Write(str);strmWriter.Write(str2);strmWriter.Write(str);strmWriter.Write(str3);strmWriter.WriteLine();//换行}strmWriter.Flush();strmWriter.Close();if(con.State==ConnectionState.Open){con.Close();}
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
12345678910ASP读取Excel文件的例子_ASP教程
您的当前位置:
ASP读取Excel文件的例子
ASP不但可以读取ACCESS、SQLSERVER,也可以读取Excel文件,只要按照一定的规则来是完全可以读取成功的。下面以Excel2000为例,实现与Excel的连接并读取内容。
其实Excel的读取与Access很相似,Excel的XLS文件就是数据库名,Excel的Sheet表其实就是Access数据库的表名,sheel表中的标题可看成是字段名。不过要注意,如果你的Excel表格中某一列同时包含了文本和数字的话,那么Excel的ODBC驱动将不能正常处理这一行的数据类型,你必须要保证该列的数据类型一致。
Dim Conn,Driver,DBPath,Rs
'建立Excel的Connection对象
Set Conn = Server.CreateObject(&ADODB.Connection&)
Driver = &Driver={Microsoft Excel Driver (*.xls)};&
DBPath = &DBQ=& & Server.MapPath( &test.xls& )
'调用Open方法打开数据库
Conn.Open Driver & DBPath
'设置DSN连接方式
'Conn.Open &Dsn=test&
'注意 表名一定要按照 &[表名$]& 格式书写
Sql=&Select * From [Sheet1$] where 序号=0&
Set Rs=Conn.Execute(Sql)
IF Rs.Eof And Rs.Bof Then
 Response.write &数据未找到!!&
 Do While Not Rs.EOF
  Response.write Rs(&书名&)
 Rs.MoveNextasp操作Excel类 - 网络编程 - 蓝色理想
您的位置:  &
& asp操作Excel类
 asp操作Excel类
作者: 时间:  文档类型:原创 来自:
asp操作Excel类:
&%'*******************************************************************'使用说明'Dim a'Set a=new CreateExcel'a.SavePath="x" '保存路径'a.SheetName="工作簿名称"& &&&&&'多个工作表 a.SheetName=array("工作簿名称一","工作簿名称二")'a.SheetTitle="表名称" &&&&&&&&'可以为空&&多个工作表 a.SheetName=array("表名称一","表名称二")'a.Data =d '二维数组 &&&&&&&&&&&&'多个工作表 array(b,c) b与c为二维数组'Dim rs'Set rs=server.CreateObject("Adodb.RecordSet")'rs.open "Select id, classid, className from [class] ",conn, 1, 1'a.AddDBData rs, "字段名一,字段名二", "工作簿名称", "表名称", &&&&true& & 'true自动获取表字段名'a.AddData c, true , "工作簿名称", "表名称"& & 'c二维数组&&&&&&&&&&true&&第一行是否为标题行'a.AddtData e, "Sheet1"& &'按模板生成&&c=array(array("AA1", "内容"), array("AA2", "内容2"))'a.Create()'a.UsedTime&&&&&&&&生成时间,毫秒数'a.SavePath&&&&&&&&保存路径'Set a=nothing'设置COM组件的操作权限。在命令行键入“DCOMCNFG”,则进入COM组件配置界面,选择MicrosoftExcel后点击属性按钮,将三个单选项一律选择自定义,编辑中将Everyone加入所有权限'*******************************************************************Class CreateExcel &&&&Private CreateType_&&&&Private savePath_&&&&Private readPath_&&&&Private AuthorStr&&&&&&&&&&&&&&Rem 设置作者&&&&Private VersionStr&&&&&&&&&&Rem 设置版本&&&&Private SystemStr&&&&&&&&&&&&&&Rem 设置系统名称&&&&Private SheetName_ &&&&&&&&&&&&Rem 设置表名&&&&Private SheetTitle_ &&&&&&&&Rem 设置标题&&&&Private ExcelData &&&&&&&&&&&&Rem 设置表数据&&&&Private ExcelApp &&&&&&&&&&&&Rem Excel.Application&&&&Private ExcelBook&&&&Private ExcelSheets&&&&Private UsedTime_&&&&&&&&&&&&Rem 使用的时间&&&&Public TitleFirstLine&&&&&&&&Rem 首行是否标题&&&&Private Sub Class_Initialize()&&&&&&&&Server.ScriptTimeOut = 99999&&&&&&&&UsedTime_ = Timer&&&&&&&&SystemStr&&&&&&&&&&&&=&&&&"Lc00_CreateExcelServer"&&&&&&&&AuthorStr&&&&&&&&&&&&=&&&&"Surnfu&&&&"&&&&&&&&VersionStr&&&&&&&&&&&&=&&&&"1.0"&&&&&&&&if not IsObjInstalled("Excel.Application") then&&&&&&&&&&&&InErr("服务器未安装Excel.Application控件")&&&&&&&&end if&&&&&&&&set ExcelApp = createObject("Excel.Application")&&&&&&&&ExcelApp.DisplayAlerts = false&&&&&&&&ExcelApp.Application.Visible = false&&&&&&&&CreateType_ = 1&&&&&&&&readPath_ = null&&&&End Sub &&&&Private Sub Class_Terminate()&&&&&&&&ExcelApp.Quit&&&&&&&&If Isobject(ExcelSheets) &&&&Then Set ExcelSheets&&&&=&&&&Nothing&&&&&&&&If Isobject(ExcelBook) &&&&&&&&Then Set ExcelBook&&&&&&&&=&&&&Nothing&&&&&&&&If Isobject(ExcelApp) &&&&&&&&Then Set ExcelApp&&&&&&&&=&&&&Nothing&&&&End Sub &&&&Public Property Let ReadPath(ByVal Val)&&&&&&&&If Instr(Val, ":\")&&0 Then&&&&&&&&&&&&readPath_ = Trim(Val)&&&&&&&&else&&&&&&&&&&&&readPath_=Server.MapPath(Trim(Val))&&&&&&&&end if&&&&End Property&&&&Public Property Let SavePath(ByVal Val)&&&&&&&&If Instr(Val, ":\")&&0 Then&&&&&&&&&&&&savePath_ = Trim(Val)&&&&&&&&else&&&&&&&&&&&&savePath_=Server.MapPath(Trim(Val))&&&&&&&&end if&&&&End Property&&&&&&&&&&&&Public Property Let CreateType(ByVal Val)&&&&&&&&if Val && 1 and Val && 2 then&&&&&&&&&&&&CreateType_ = 1&&&&&&&&else&&&&&&&&&&&&CreateType_ = Val&&&&&&&&end if&&&&&&&&End Property&&&&&&&&Public Property Let Data(ByVal Val)&&&&&&&&if not isArray(Val) then&&&&&&&&&&&&InErr("表数据设置有误")&&&&&&&&end if&&&&&&&&&&ExcelData = Val&&&&End Property&&&&Public Property Get SavePath()&&&&SavePath = savePath_&&&&End Property&&&&Public Property Get UsedTime()&&&&&&&&&&UsedTime = UsedTime_&&&&End Property&&&&Public Property Let SheetName(ByVal Val)&&&&&&&&if not isArray(Val) then&&&&&&&&&&&&if Val = "" then&&&&&&&&&&&&&&&&InErr("表名设置有误")&&&&&&&&&&&&end if&&&&&&&&&&&&TitleFirstLine = true&&&&&&&&else&&&&&&&&&&&&ReDim TitleFirstLine(Ubound(Val))&&&&&&&&&&&&Dim ik_&&&&&&&&&&&&For ik_ = 0 to Ubound(Val)&&&&&&&&&&&&&&&&TitleFirstLine(ik_) = true&&&&&&&&&&&&Next&&&&&&&&end if&&&&&&&&&&SheetName_ = Val&&&&End Property&&&&&&&&Public Property Let SheetTitle(ByVal Val)&&&&&&&&if not isArray(Val) then&&&&&&&&&&&&if Val = "" then&&&&&&&&&&&&&&&&InErr("表标题设置有误")&&&&&&&&&&&&end if&&&&&&&&end if&&&&&&&&&&SheetTitle_ = Val&&&&End Property&&&&&&&&Rem 检查数据&&&&Private Sub CheckData()&&&&&&&&if savePath_ = "" then InErr("保存路径不能为空")&&&&&&&&if not isArray(SheetName_) then&&&&&&&&&&&&if SheetName_ = "" then InErr("表名不能为空")&&&&&&&&end if&&&&&&&&&&&&&&&&if CreateType_ = 2 then&&&&&&&&&&&&if not isArray(ExcelData) then&&&&&&&&&&&&&&&&InErr("数据载入错误,或者未载入")&&&&&&&&&&&&end if&&&&&&&&&&&&Exit Sub&&&&&&&&end if&&&&&&&&&&&&&&&&if isArray(SheetName_) then&&&&&&&&&&&&if not isArray(SheetTitle_) then&&&&&&&&&&&&&&&&if SheetTitle_ && "" then InErr("表标题设置有误,与表名不对应")&&&&&&&&&&&&end if&&&&&&&&end if&&&&&&&&if not IsArray(ExcelData) then&&&&&&&&&&&&InErr("表数据载入有误")&&&&&&&&end if&&&&&&&&if isArray(SheetName_) then&&&&&&&&&&&&if GetArrayDim(ExcelData) && 1 then InErr("表数据载入有误,数据格式错误,维度应该为一")&&&&&&&&else&&&&&&&&&&&&if GetArrayDim(ExcelData) && 2 then InErr("表数据载入有误,数据格式错误,维度应该为二")&&&&&&&&end if&&&&End Sub&&&&Rem 生成Excel&&&&Public Function Create()&&&&&&&&Call CheckData()&&&&&&&&if not isnull(readPath_) then&&&&&&&&&&&&ExcelApp.WorkBooks.Open(readPath_) &&&&&&&&else&&&&&&&&&&&&ExcelApp.WorkBooks.add&&&&&&&&end if&&&&&&&&&&&&&&&&set ExcelBook = ExcelApp.ActiveWorkBook&&&&&&&&set ExcelSheets = ExcelBook.Worksheets&&&&&&&&&&&&&&&&if CreateType_ = 2 then&&&&&&&&&&&&Dim ih_&&&&&&&&&&&&For ih_ = 0 to Ubound(ExcelData)&&&&&&&&&&&&&&&&Call SetSheets(ExcelData(ih_), ih_)&&&&&&&&&&&&Next&&&&&&&&&&&&ExcelBook.SaveAs savePath_&&&&&&&&&&&&UsedTime_ = FormatNumber((Timer - UsedTime_)*1000, 3)&&&&&&&&&&&&Exit Function&&&&&&&&end if&&&&&&&&&&&&&&&&if IsArray(SheetName_) then&&&&&&&&&&&&Dim ik_&&&&&&&&&&&&For ik_ = 0 to Ubound(ExcelData)&&&&&&&&&&&&&&&&Call CreateSheets(ExcelData(ik_), ik_)&&&&&&&&&&&&Next&&&&&&&&else&&&&&&&&&&&&Call CreateSheets(ExcelData, -1)&&&&&&&&end if&&&&&&&&&&&&&&&&ExcelBook.SaveAs savePath_&&&&&&&&UsedTime_ = FormatNumber((Timer - UsedTime_)*1000, 3)&&&&End Function &&&&Private Sub CreateSheets(ByVal Data_, DataId_)&&&&&&&&Dim Spreadsheet&&&&&&&&Dim tempSheetTitle&&&&&&&&Dim tempTitleFirstLine&&&&&&&&if DataId_&&-1 then&&&&&&&&&&&&if DataId_ & ExcelSheets.Count - 1 then&&&&&&&&&&&&&&&&ExcelSheets.Add()&&&&&&&&&&&&&&&&set Spreadsheet = ExcelBook.Sheets(1)&&&&&&&&&&&&else&&&&&&&&&&&&&&&&set Spreadsheet = ExcelBook.Sheets(DataId_ + 1)&&&&&&&&&&&&end if&&&&&&&&&&&&if isArray(SheetTitle_) then&&&&&&&&&&&&&&&&tempSheetTitle = SheetTitle_(DataId_)&&&&&&&&&&&&else&&&&&&&&&&&&&&&&tempSheetTitle = ""&&&&&&&&&&&&end if&&&&&&&&&&&&tempTitleFirstLine = TitleFirstLine(DataId_)&&&&&&&&&&&&Spreadsheet.Name = SheetName_(DataId_)&&&&&&&&else&&&&&&&&&&&&set Spreadsheet = ExcelBook.Sheets(1)&&&&&&&&&&&&Spreadsheet.Name = SheetName_&&&&&&&&&&&&tempSheetTitle = SheetTitle_&&&&&&&&&&&&tempTitleFirstLine = TitleFirstLine&&&&&&&&end if&&&&&&&&Dim Line_ : Line_ = 1&&&&&&&&Dim RowNum_ : RowNum_ = Ubound(Data_, 1) + 1&&&&&&&&Dim LastCols_&&&&&&&&if tempSheetTitle && "" then&&&&&&&&&&&&'Spreadsheet.Columns(1).ShrinkToFit=true '设定是否自动适应表格单元大小(单元格宽不变)&&&&&&&&&&&&LastCols_ = getColName(Ubound(Data_, 2) + 1)&&&&&&&&&&&&with Spreadsheet.Cells(1, 1)&&&&&&&&&&&&&&&&.value = tempSheetTitle&&&&&&&&&&&&&&&&'设置Excel表里的字体 &&&&&&&&&&&&&&&&.Font.Bold = True '单元格字体加粗&&&&&&&&&&&&&&&&.Font.Italic = False '单元格字体倾斜&&&&&&&&&&&&&&&&.Font.Size = 20 '设置单元格字号&&&&&&&&&&&&&&&&.font.name="宋体" '设置单元格字体&&&&&&&&&&&&&&&&'.font.ColorIndex=2 '设置单元格文字的颜色,颜色可以查询,2为白色&&&&&&&&&&&&End with&&&&&&&&&&&&with Spreadsheet.Range("A1:"& LastCols_ &"1")&&&&&&&&&&&&&&&&.merge '合并单元格(单元区域)&&&&&&&&&&&&&&&&'.Interior.ColorIndex = 1 '设计单元络背景色&&&&&&&&&&&&&&&&.HorizontalAlignment = 3 '居中&&&&&&&&&&&&End with&&&&&&&&&&&&Line_ = 2&&&&&&&&&&&&RowNum_ = RowNum_ + 1&&&&&&&&end if&&&&&&&&Dim iRow_, iCol_&&&&&&&&Dim dRow_, dCol_&&&&&&&&Dim tempLastRange : tempLastRange = getColName(Ubound(Data_, 2)+1) & (RowNum_)&&&&&&&&&&&&&&&&Dim BeginRow : BeginRow = 1&&&&&&&&if tempSheetTitle && "" then BeginRow = BeginRow + 1&&&&&&&&if tempTitleFirstLine = true then BeginRow = BeginRow + 1&&&&&&&&&&&&&&&&if BeginRow=1 then&&&&&&&&&&&&with Spreadsheet.Range("A1:"& tempLastRange)&&&&&&&&&&&&&&&&.Borders.LineStyle = 1&&&&&&&&&&&&&&&&.BorderAround -4119, -4138 '设置外框&&&&&&&&&&&&&&&&.NumberFormatLocal = "@"& &'文本格式&&&&&&&&&&&&&&&&.Font.Bold = False &&&&&&&&&&&&&&&&.Font.Italic = False &&&&&&&&&&&&&&&&.Font.Size = 10&&&&&&&&&&&&&&&&.ShrinkToFit=true &&&&&&&&&&&&end with&&&&&&&&else&&&&&&&&&&&&with Spreadsheet.Range("A1:"& tempLastRange)&&&&&&&&&&&&&&&&.Borders.LineStyle = 1&&&&&&&&&&&&&&&&.BorderAround -4119, -4138&&&&&&&&&&&&&&&&.ShrinkToFit=true &&&&&&&&&&&&end with&&&&&&&&&&&&&&&&&&&&&&&&with Spreadsheet.Range("A"& BeginRow &":"& tempLastRange)&&&&&&&&&&&&&&&&.NumberFormatLocal = "@" &&&&&&&&&&&&&&&&.Font.Bold = False &&&&&&&&&&&&&&&&.Font.Italic = False &&&&&&&&&&&&&&&&.Font.Size = 10&&&&&&&&&&&&end with&&&&&&&&end if&&&&&&&&&&&&&&&&if tempTitleFirstLine = true then&&&&&&&&&&&&BeginRow = 1&&&&&&&&&&&&if tempSheetTitle && "" then BeginRow = BeginRow + 1&&&&&&&&&&&&&&&&&&&&with Spreadsheet.Range("A"& BeginRow &":"& getColName(Ubound(Data_, 2)+1) & (BeginRow))&&&&&&&&&&&&&&&&.NumberFormatLocal = "@"&&&&&&&&&&&&&&&&.Font.Bold = True &&&&&&&&&&&&&&&&.Font.Italic = False &&&&&&&&&&&&&&&&.Font.Size = 12&&&&&&&&&&&&&&&&.Interior.ColorIndex = 37&&&&&&&&&&&&&&&&.HorizontalAlignment = 3 '居中&&&&&&&&&&&&&&&&.font.ColorIndex=2&&&&&&&&&&&&end with&&&&&&&&end if&&&&&&&&&&&&&&&&For iRow_ = Line_ To RowNum_&&&&&&&&&&&&For iCol_ = 1 To (Ubound(Data_, 2) + 1)&&&&&&&&&&&&&&&&dCol_ = iCol_ - 1&&&&&&&&&&&&&&&&if tempSheetTitle && "" then dRow_ = iRow_ - 2 else dRow_ = iRow_ - 1&&&&&&&&&&&&&&&&If not IsNull(Data_(dRow_, dCol_)) then &&&&&&&&&&&&&&&&&&&&with Spreadsheet.Cells(iRow_, iCol_)&&&&&&&&&&&&&&&&&&&&&&&&.Value = Data_(dRow_, dCol_)&&&&&&&&&&&&&&&&&&&&End with&&&&&&&&&&&&&&&&End If &&&&&&&&&&&&Next&&&&&&&&Next&&&&&&&&set Spreadsheet = Nothing&&&&End Sub &&&&Rem 测试组件是否已经安装&&&&Private Function IsObjInstalled(strClassString)&&&&&&&&On Error Resume Next&&&&&&&&IsObjInstalled = False&&&&&&&&Err = 0&&&&&&&&Dim xTestObj&&&&&&&&Set xTestObj = Server.CreateObject(strClassString)&&&&&&&&If 0 = Err Then IsObjInstalled = True&&&&&&&&Set xTestObj = Nothing&&&&&&&&Err = 0&&&&End Function&&&&Rem 取得数组维数&&&&Private Function GetArrayDim(ByVal arr)& &&&&&&&&&GetArrayDim = Null& &&&&&&&&&Dim i_, temp& &&&&&&&&&If IsArray(arr) Then&&&&&&&&&&&&&&For i_ = 1 To 60& &&&&&&&&&&&&&&&&&On Error Resume Next&&&&&&&&&&&&&&&&&&temp = UBound(arr, i_)& &&&&&&&&&&&&&&&&&If Err.Number && 0 Then&&&&&&&&&&&&&&&&&&&&&&GetArrayDim = i_ - 1&&&&&&&&&&&&&&&&&&&&Err.Clear &&&&&&&&&&&&&&&&&&&&Exit Function&&&&&&&&&&&&&&&&&&End If&&&&&&&&&&&&&&Next&&&&&&&&&&&&&&GetArrayDim = i_& &&&&&&&&&End If&&&&&&End Function &&&&Private Function GetNumFormatLocal(DataType)&&&&&&&&Select Case DataType&&&&&&&&&&&&Case "Currency":&&&&&&&&&&&&&&&&GetNumFormatLocal = "¥#,##0.00_);(¥#,##0.00)"&&&&&&&&&&&&Case "Time":&&&&&&&&&&&&&&&&GetNumFormatLocal = "[$-F800]dddd, mmmm dd, yyyy"&&&&&&&&&&&&Case "Char":&&&&&&&&&&&&&&&&GetNumFormatLocal = "@"&&&&&&&&&&&&Case "Common":&&&&&&&&&&&&&&&&GetNumFormatLocal = "G/通用格式"&&&&&&&&&&&&Case "Number":&&&&&&&&&&&&&&&&GetNumFormatLocal = "#,##0.00_"&&&&&&&&&&&&Case else :&&&&&&&&&&&&&&&&GetNumFormatLocal = "@"&&&&&&&&End Select&&&&End Function&&&&Public Sub AddDBData(ByVal RsFlied, ByVal FliedTitle, ByVal tempSheetName_, ByVal tempSheetTitle_, DBTitle)&&&&&&&&if RsFlied.Eof then Exit Sub&&&&&&&&Dim colNum_ : colNum_ = RsFlied.fields.count&&&&&&&&Dim Rownum_ : Rownum_ = RsFlied.RecordCount&&&&&&&&Dim ArrFliedTitle&&&&&&&&&&&&&&&&if DBTitle = true then&&&&&&&&&&&&FliedTitle = ""&&&&&&&&&&&&Dim ig_&&&&&&&&&&&&For ig_=0 to colNum_ - 1&&&&&&&&&&&&&&&&FliedTitle = FliedTitle & RsFlied.fields.item(ig_).name&&&&&&&&&&&&&&&&if ig_ && colNum_ - 1 then FliedTitle = FliedTitle &","&&&&&&&&&&&&Next&&&&&&&&end if&&&&&&&&&&&&&&&&if FliedTitle&&"" then&&&&&&&&&&&&Rownum_ = Rownum_ + 1&&&&&&&&&&&&ArrFliedTitle = Split(FliedTitle, ",")&&&&&&&&&&&&if Ubound(ArrFliedTitle) && colNum_ - 1&&then&&&&&&&&&&&&&&&&InErr("获取数据库表有误,列数不符")&&&&&&&&&&&&end if&&&&&&&&end if&&&&&&&&&&&&Dim tempData : ReDim tempData(Rownum_ - 1, colNum_ - 1)&&&&&&&&&&&&&&&&Dim ix_, iy_&&&&&&&&Dim iz&&&&&&&&if FliedTitle&&"" then iz = Rownum_ - 2&&else iz = Rownum_ - 1&&&&&&&&&&&&&&&&For ix_ = 0 To iz&&&&&&&&&&&&For iy_ = 0 To colNum_ - 1&&&&&&&&&&&&&&&&if FliedTitle&&"" then&&&&&&&&&&&&&&&&&&&&if ix_=0 then&&&&&&&&&&&&&&&&&&&&&&&&tempData(ix_, iy_) = ArrFliedTitle(iy_)&&&&&&&&&&&&&&&&&&&&&&&&tempData(ix_ + 1, iy_) = RsFlied(iy_)&&&&&&&&&&&&&&&&&&&&else&&&&&&&&&&&&&&&&&&&&&&&&tempData(ix_ + 1, iy_) = RsFlied(iy_)&&&&&&&&&&&&&&&&&&&&end if&&&&&&&&&&&&&&&&else&&&&&&&&&&&&&&&&&&&&tempData(ix_, iy_) = RsFlied(iy_)&&&&&&&&&&&&&&&&end if&&&&&&&&&&&&Next&&&&&&&&&&&&RsFlied.MoveNext&&&&&&&&Next&&&&&&&&&&&&&&&&Dim tempFirstLine &&&&&&&&if FliedTitle&&"" then tempFirstLine = true else tempFirstLine = false&&&&&&&&Call AddData(tempData, tempFirstLine, tempSheetName_, tempSheetTitle_)&&&&End Sub&&&&Public Sub AddData(ByVal tempDate_, ByVal tempFirstLine_, ByVal tempSheetName_, ByVal tempSheetTitle_)&&&&&&&&if not isArray(ExcelData) then&&&&&&&&&&&&ExcelData = tempDate_&&&&&&&&&&&&TitleFirstLine = tempFirstLine_&&&&&&&&&&&&SheetName_ = tempSheetName_&&&&&&&&&&&&SheetTitle_ = tempSheetTitle_&&&&&&&&else&&&&&&&&&&&&if GetArrayDim(ExcelData) = 1 then&&&&&&&&&&&&&&&&Dim tempArrLen : tempArrLen = Ubound(ExcelData)+1&&&&&&&&&&&&&&&&ReDim Preserve ExcelData(tempArrLen)&&&&&&&&&&&&&&&&ExcelData(tempArrLen) = tempDate_&&&&&&&&&&&&&&&&ReDim Preserve TitleFirstLine(tempArrLen)&&&&&&&&&&&&&&&&TitleFirstLine(tempArrLen) = tempFirstLine_&&&&&&&&&&&&&&&&ReDim Preserve SheetName_(tempArrLen)&&&&&&&&&&&&&&&&SheetName_(tempArrLen) = tempSheetName_&&&&&&&&&&&&&&&&ReDim Preserve SheetTitle_(tempArrLen)&&&&&&&&&&&&&&&&SheetTitle_(tempArrLen) = tempSheetTitle_&&&&&&&&&&&&else&&&&&&&&&&&&&&&&Dim tempOldData : tempOldData = ExcelData&&&&&&&&&&&&&&&&ExcelData = Array(tempOldData, tempDate_)&&&&&&&&&&&&&&&&TitleFirstLine = Array(TitleFirstLine, tempFirstLine_)&&&&&&&&&&&&&&&&SheetName_ = Array(SheetName_, tempSheetName_)&&&&&&&&&&&&&&&&SheetTitle_ = Array(SheetTitle_, tempSheetTitle_)&&&&&&&&&&&&end if&&&&&&&&end if&&&&End Sub&&&&Rem 模板增加数据方法&&&&Public Sub AddtData(ByVal tempDate_, ByVal tempSheetName_)&&&&&&&&CreateType_ = 2&&&&&&&&if not isArray(ExcelData) then&&&&&&&&&&&&ExcelData = Array(tempDate_)&&&&&&&&&&&&SheetName_ = Array(tempSheetName_)&&&&&&&&else&&&&&&&&&&&&Dim tempArrLen : tempArrLen = Ubound(ExcelData)+1&&&&&&&&&&&&ReDim Preserve ExcelData(tempArrLen)&&&&&&&&&&&&ExcelData(tempArrLen) = tempDate_&&&&&&&&&&&&ReDim Preserve SheetName_(tempArrLen)&&&&&&&&&&&&SheetName_(tempArrLen) = tempSheetName_&&&&&&&&End if&&&&End Sub&&&&Private Sub SetSheets(ByVal Data_, DataId_)&&&&&&&&Dim Spreadsheet&&&&&&&&set Spreadsheet = ExcelBook.Sheets(SheetName_(DataId_))&&&&&&&&Spreadsheet.Activate&&&&&&&&Dim ix_&&&&&&&&For ix_ =0 To Ubound(Data_)&&&&&&&&&&&&if not isArray(Data_(ix_)) then InErr("表数据载入有误,数据格式错误")&&&&&&&&&&&&if Ubound(Data_(ix_)) && 1 then InErr("表数据载入有误,数据格式错误")&&&&&&&&&&&&Spreadsheet.Range(Data_(ix_)(0)).value = Data_(ix_)(1)&&&&&&&&Next&&&&&&&&set Spreadsheet = Nothing&&&&End Sub&&&&Public Function GetTime(msec_)&&&&&&&&Dim ReTime_ : ReTime_=""&&&&&&&&if msec_ & 1000 then&&&&&&&&&&&&ReTime_ = msec_ &"MS"&&&&&&&&else&&&&&&&&&&&&Dim second_&&&&&&&&&&&&second_ = (msec_ \ 1000)&&&&&&&&&&&&if (msec_ mod 1000)&&0 then&&&&&&&&&&&&&&&&msec_ = (msec_ mod 1000) &"毫秒"&&&&&&&&&&&&else&&&&&&&&&&&&&&&&msec_ = ""&&&&&&&&&&&&end if&&&&&&&&&&&&Dim n_, aryTime(2), aryTimeunit(2)&&&&&&&&&&&&aryTimeunit(0) = "秒"&&&&&&&&&&&&aryTimeunit(1) = "分"&&&&&&&&&&&&aryTimeunit(2) = "小时"&&&&&&&&&&&&n_ = 0&&&&&&&&&&&&Dim tempSecond_ : tempSecond_ = second_&&&&&&&&&&&&While(tempSecond_ / 60 &= 1)&&&&&&&&&&&&&&&&tempSecond_ = Fix(tempSecond_ / 60 * 100) / 100&&&&&&&&&&&&&&&&n_ = n_ + 1&&&&&&&&&&&&WEnd&&&&&&&&&&&&Dim m_&&&&&&&&&&&&For m_ = n_ To 0 Step -1&&&&&&&&&&&&&&&&aryTime(m_) = second_ \ (60 ^ m_)&&&&&&&&&&&&&&&&second_ = second_ mod (60 ^ m_)&&&&&&&&&&&&&&&&ReTime_ = ReTime_ & aryTime(m_) & aryTimeunit(m_)&&&&&&&&&&&&Next&&&&&&&&&&&&if msec_&&"" then ReTime_ = ReTime_ & msec_&&&&&&&&end if&&&&&&&&GetTime = ReTime_ &&&&end Function&&&&Rem 取得列名&&&&Private Function getColName(ByVal ColNum)&&&&&&&&Dim Arrlitter : Arrlitter=split("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", " ")&&&&&&&&Dim ReValue_&&&&&&&&if ColNum &= Ubound(Arrlitter) + 1 then &&&&&&&&&&&&ReValue_ = Arrlitter(ColNum - 1)&&&&&&&&else&&&&&&&&&&&&ReValue_ = Arrlitter(((ColNum-1) \ 26)) & Arrlitter(((ColNum-1) mod 26))&&&&&&&&end if&&&&&&&&getColName = ReValue_&&&&End Function&&&&Rem 设置错误&&&&Private Sub InErr(ErrInfo)&&&&&&&&Err.Raise vbObjectError + 1, SystemStr &"(Version "& VersionStr &")", ErrInfo&&&&End SubEnd ClassDim b(4,6)Dim c(50,20)Dim i, jFor i=0 to 4&&&&For j=0 to 6&&&&&&&&b(i,j) =i&"-"&j&&&&NextNextFor i=0 to 50&&&&For j=0 to 20&&&&&&&&c(i,j) = i&"-"&j &"我的"&&&&NextNextDim e(20)For i=0 to 20&&&&e(i)= array("A"&(i+1), i+1)Next'使用示例&&需要xx.xls模板支持'Set a=new CreateExcel'a.ReadPath = "xx.xls"'a.SavePath="xx-1.xls"'a.AddtData e, "Sheet1"'a.Create()'response.Write("生成"& a.SavePath &" 使用了 "& a.GetTime(a.UsedTime) &"&br&")'Set a=nothing'使用示例一Set a=new CreateExcela.SavePath="x.xls"a.AddData b, true , "测试c", "测试c"a.TitleFirstLine = false '首行是否为标题行a.Create()response.Write("生成"& a.SavePath &" 使用了 "& a.GetTime(a.UsedTime) &"&br&")Set a=nothing'使用示例二Set a=new CreateExcela.SavePath="y.xls"a.SheetName="工作簿名称"& &&&&&'多个工作表 a.SheetName=array("工作簿名称一","工作簿名称二")a.SheetTitle="表名称" &&&&&&&&'可以为空&&多个工作表 a.SheetName=array("表名称一","表名称二")a.Data =b '二维数组 &&&&&&&&&&&&'多个工作表 array(b,c) b与c为二维数组a.Create()response.Write("生成"& a.SavePath &" 使用了 "& a.GetTime(a.UsedTime) &"&br&")Set a=nothing'使用示例三 生成两个表Set a=new CreateExcela.SavePath="z.xls"a.SheetName=array("工作簿名称一","工作簿名称二")a.SheetTitle=array("表名称一","表名称二")a.Data =array(b, c) 'b与c为二维数组a.TitleFirstLine = array(false, true) '首行是否为标题行a.Create()response.Write("生成"& a.SavePath &" 使用了 "& a.GetTime(a.UsedTime) &"&br&")Set a=nothing'使用示例四&&&&需要数据库支持'Dim rs'Set rs=server.CreateObject("Adodb.RecordSet")'rs.open "Select id, classid, className from [class] ",conn, 1, 1'Set a=new CreateExcel'a.SavePath="a"'a.AddDBData rs, "序号,类别序号,类别名称", "工作簿名称", "类别表", false'a.Create()'response.Write("生成"& a.SavePath &" 使用了 "& a.GetTime(a.UsedTime) &"&br&")'Set a=nothing'rs.close'Set rs=nothing%&
经典论坛交流:
本文链接: 
责任编辑:
◎进入论坛版块参加讨论
蓝色理想版权申明:除部分特别声明不要转载,或者授权我站独家播发的文章外,大家可以自由转载我站点的原创文章,但原作者和来自我站的链接必须保留(非我站原创的,按照原来自一节,自行链接)。文章版权归我站和作者共有。
转载要求:转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印,亦不能抹去我站点水印。
特别注意:本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有,文章若有侵犯作者版权,请与我们,我们将立即删除修改。
本文暂时没有评论和评分
说明:输入正确的用户名和密码才能参与评论。如果您不是本站会员,你可以 为本站会员。
注意:文章中的链接、内容等需要修改的错误,请用,以利文档及时修改。
注意:请不要在评论中含与内容无关的广告链接,违者封ID
请您注意:?不良评论请用,以利管理员及时删除。?尊重网上道德,遵守中华人民共和国的各项有关法律法规?承担一切因您的行为而直接或间接导致的民事或刑事法律责任?本站评论管理人员有权保留或删除其管辖评论中的任意内容
?您在本站发表的作品,本站有权在网站内转载或引用 ?参与本评论即表明您已经阅读并接受上述条款
专业书推荐
&1999-. 版权所有

我要回帖

更多关于 asp读取excel文件内容 的文章

 

随机推荐