Excel匯入匯出,javaexcel匯入匯出
using System;using System.Collections.Generic;using System.Linq;using System.Web;using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using System.IO;using System.Data;namespace Excel匯出及讀取{ /// <summary> /// Excel匯出及讀取 的摘要說明 /// </summary> public class Excel匯出及讀取 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/x-excel"; //------------------------------------------將資料寫入Excel-------- HSSFWorkbook workbook = new HSSFWorkbook();//建立一個Excel檔案 ISheet sheet1 = workbook.CreateSheet("Sheet1");//建立一個頁 IRow rows = sheet1.CreateRow(0); //建立第0行 //在第0行的第0列裡面建立一個string類型的儲存格,並給它賦值為"C罩杯"; rows.CreateCell(0, CellType.String).SetCellValue("C罩杯"); rows.CreateCell(1, CellType.String).SetCellValue("D罩杯"); rows.CreateCell(2, CellType.String).SetCellValue("A罩杯"); rows.CreateCell(3, CellType.String).SetCellValue("F罩杯"); //得到一個excel.xls檔案的檔案流 【OpenWrite()開啟現有檔案以進行寫入】 using (Stream stream = File.OpenWrite("d:/excel.xls")) { //將這個hssfworkbook檔案寫入到excel.xls這個檔案流中 workbook.Write(stream); } //------------------------------------------讀取Excel的資料------------- using (Stream stream1 = File.OpenRead("d:/excel.xls")) { //讀取workbook這個工作薄的第0個Sheet(GetSheetAt(0)),第0行(GetRow(0)),第0格(GetCell(0))的值 //string s = workbook.GetSheetAt(0).GetRow(0).GetCell(0).StringCellValue; //擷取excel的第一個sheet ISheet Mysheet = workbook.GetSheetAt(0); //擷取Mysheet的首行 IRow row = Mysheet.GetRow(0); //一行最後一個方格的編號 即總的列數 int cellCount = rows.LastCellNum; //最後一列的標號 即總的行數 int cellRows = sheet1.LastRowNum; //--------------------------------------建立一個新的workbook2工作薄,並將workbook的內容複寫到workbook2中 HSSFWorkbook workbook2 = new HSSFWorkbook();//建立一個Excel檔案 ISheet sheet2 = workbook2.CreateSheet("Sheet1");//建立一個頁 //IRow rows2 = sheet2.CreateRow(0); //建立第0行 //根據sheet頁的總行數,來建立sheet2頁的總行數 for (int i = 0; i <= cellRows; i++) { IRow sheet2Rows = sheet2.CreateRow(i); //根據shee1頁的總列數,建立shee2頁的總列數 for (int j = 0; j < cellCount; j++) { //DataColumn column=new DataColumn string ss = workbook.GetSheetAt(0).GetRow(i).GetCell(j).StringCellValue; sheet2Rows.CreateCell(j, CellType.String).SetCellValue(ss); //context.Response.Write(cellCount); } } using (Stream stream = File.OpenWrite("d:/excel2.xls")) { //將這個workbook2檔案寫入到excel2.xls這個檔案流中 workbook2.Write(stream); } } } public bool IsReusable { get { return false; } } }}
excel 匯入匯出是什
包括excel在內的辦公文檔其實有很多格式,例如txt,pdf等,單單試算表,其實也有很多種形式。匯入匯出功能就是使excel可以接收或者轉換成這些形式。
excel匯入匯出問題
思路:用程式將excel檔案的內容讀記錄出來,然後將其寫入伺服器資料庫
不知道你用的是什麼平台,這裡提供一個asp環境參考:
在asp中讀excel檔案記錄:
Set connExcel = Server.CreateObject("ADODB.Connection")
connExcel.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath( "InputExcel\sample.xls" ) &";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';"
connExcel.Open connStr
Set Conn2= Server.CreateObject("ADODB.Connection")
Conn2.ConnectionString="driver={SQL Server};server=127.0.0.1;database=chicken;uid=sa;pwd=88256373"
Conn2.Open
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "Select * From [sheet1$]", connExcel, 0, 1
do while not rs.eof
...
'在這裡可以加上insert into將等記錄寫入資料庫
Conn2.excute("insert into(,,,,)")
rs.moveNext
loop
rs.close
set rs=nothing