Excel import and export, javaexcel Import and Export
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 export and read {// <summary> // abstract for Excel export and read /// </summary> public class Excel export and read: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "application/x-excel"; // submit write data to Excel -------- HSSFWorkbook workbook = new HSSFWorkbook (); // create an Excel file ISheet sheet1 = workbook. createSheet ("Sheet1"); // create a page IRow rows = sheet1.CreateRow (0 ); // create row 0th // create a string-type cell in column 0th of row 0th and assign it "C Cup"; rows. createCell (0, CellType. string ). setCellValue ("C Cup"); rows. createCell (1, CellType. string ). setCellValue ("D cup"); rows. createCell (2, CellType. string ). setCellValue ("A Cup"); rows. createCell (3, CellType. string ). setCellValue ("F Cup"); // get the File Stream of an excel.xls File [OpenWrite () Open existing File for writing] using (stream Stream = File. openWrite ("d:/excel.xls") {// write this hssfworkbookfile to workbook in the file stream excel.xls. write (stream);} // ---------------------------------------------- read Excel Data ------------- using (Stream stream1 = File. openRead ("d:/excel.xls") {// read The 0th Sheet (GetSheetAt (0), 0th rows (GetRow (0) of the workbook )), value of The 0th lattice (GetCell (0) // string s = workbook. getSheetAt (0 ). getRow (0 ). getCell (0 ). stringCellValue; // obtain the first sheet ISheet Mysheet = workbook in excel. getSheetAt (0); // get the first line of Mysheet IRow = Mysheet. getRow (0); // The number of the last square in a row, that is, the total number of columns int cellCount = rows. lastCellNum; // the label of the last column, that is, the total number of rows int cellRows = sheet1.LastRowNum; // -------------------------------- create a new workbook2 working thin, copy the content of the workbook to HSSFWorkbook workbook2 = new HSSFWorkbook (); // create an Excel file ISheet sheet2 = workbook2.CreateSheet ("Sheet1 "); // create a page // IRow rows2 = sheet2.CreateRow (0); // create 0th rows // based on the total number of rows on the sheet page, for (int I = 0; I <= cellRows; I ++) {IRow sheet2Rows = sheet2.CreateRow (I ); // based on the total number of columns on the shee1 page, create the total number of columns on the shee2 page 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") {// write this workbook2file to workbook2.Write (stream); }}} public bool IsReusable {get {return false ;}}}}
What is excel import/export?
Office documents, including excel, are in many formats, such as txt and pdf. There are also many forms of workbooks. The import/export function enables excel to receive or convert data to these formats.
Excel import and export problems
Idea: Use a program to read and record the content of an excel file and then write it to the server database.
I don't know what platform you are using. Here is an asp environment reference:
Read excel file records in asp:
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 can be added here to write the same record to the database.
Conn2.excute ("insert (,,,,)")
Rs. moveNext
Loop
Rs. close
Set rs = nothing