// Initialize the object
Application APP = NULL;
Workbook mybook = NULL;
Worksheet mysheet = NULL;
String Spath = configurationmanager. receivettings ["exporttemp"]. tostring ();
String smsg = "";
Try
{
Spath + = path. getfilename (file. filename );
Try
{
File. saveas (Spath );
Modelsurveycommon omodel = new modelsurveycommon ();
// Create an object
APP = new application ();
Mybook = app. workbooks. open (Spath, 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 );
Mysheet = (worksheet) mybook. worksheets [1];
Range cellb;
String svalue, atemp = "";
Cellb = (range) mysheet. cells [1, 1]; // get the data of cell []
If (cellb. value2.tostring () = "survey_common ")
{
For (Int J = 1; j <mysheet. usedrange. Columns. Count + 1; j ++)
{
Cellb = (range) mysheet. cells [2, J];
If (cellb. value2! = "" & Cellb. value2! = NULL)
{
Atemp = atemp + cellb. value2.tostring () + "# ^ $ @@";
}
Else
{
Atemp = atemp + "# ^ $ @@";
}
}
String [] sfieldname = Common. Common. splitstring (atemp, "# ^ $ @@");
// Response. Write ("atemp:" + atemp + "<br> ");
For (INT I = 3; I <mysheet. usedrange. Rows. Count + 1; I ++)
{
Svalue = "";
For (Int J = 1; j <mysheet. usedrange. Columns. Count + 1; j ++)
{
Cellb = (range) mysheet. cells [I, j];
If (cellb. value2! = "" & Cellb. value2! = NULL)
{
Svalue = svalue + cellb. value2.tostring () + "# ^ $ @@";
}
Else
{
Svalue = svalue + "# ^ $ @@";
}
}
// Response. Write ("svalue:" + svalue );
// Response. Write ("svalue:" + svalue + "<br> ");
String [] sfiledvalue = Common. Common. splitstring (svalue, "# ^ $ @@");
Omodel. importsurvey (sfieldname, sfiledvalue );
}
Smsg = "Import successfully .";
}
Else
{
Smsg = "Table incorrect .";
}
}
Catch
{
Smsg = "import data error .";
}
}
Catch
{
Smsg = "Please choose file .";
}
// Clear the object
If (mysheet! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (mysheet );
Mysheet = NULL;
}
If (mybook! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (mybook );
Mybook = NULL;
}
// Close the object
App. workbooks. Close ();
App. Quit ();
If (app! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (APP );
APP = NULL;
}
GC. Collect (); // garbage collection
Use two arrays to import data to excel
String Spath = @ "C:/inetpub/wwwroot/test_20110222/test/report /";
String sfilename = "test _" + datetime. Now. tostring ("yyyymmddhhmmss") + ". xls ";
Spath + = sfilename;
System. Io. file. Copy (@ "C:/inetpub/wwwroot/test_20110222/test/report/test.xls", Spath, true );
Application APP = NULL;
Workbook mybook = NULL;
Worksheet mysheet = NULL;
APP = new application ();
Mybook = app. workbooks. open (Spath, 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 );
Mysheet = (worksheet) mybook. worksheets [1];
Int ROW = 10000, colcount = 30;
String [,] Ss = new string [row, colcount];
For (Int J = 1; j <row; j ++)
{
For (int K = 1; k <colcount; k ++)
{
Ss [j, k] = J + ":" + K;
// Mysheet. cells [j, k] = J + ":" + K;
}
}
Mysheet. get_range ("A2", "z10000"). value2 = SS;
Mybook. Save ();
If (mysheet! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (mysheet );
Mysheet = NULL;
}
If (mybook! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (mybook );
Mybook = NULL;
}
// Close the object
App. workbooks. Close ();
App. Quit ();
If (app! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (APP );
APP = NULL;
}
GC. Collect ();
GC. waitforpendingfinalizers ();