Home > Net >  C # export Excel slow solution
C # export Excel slow solution

Time:11-06

C # export Excel, slow, it's about 5 minutes, this how to solve the
 
# region ExcelString path;
Private void daochu ()
{
If (zhuangt=="" | | zhuangt==null)
{
//MessageBox. Show (" no data can be exported!" , "tip");
}
The else
{
Path=System. AppDomain. CurrentDomain. BaseDirectory + "sacolar storage. XLSX";
If (the File. The Exists (path))
{
//File. Delete (path);
ThreadStart entry=new ThreadStart (ex);//summation method is defined as a worker thread entry
WorkThread2=new Thread (entry);
WorkThread2. Start ();

}
The else
{
//SaveFileDialog SFD=new SaveFileDialog ();
//the SFD. The FileName="sacolar storage".//file name
//SFD. DefaultExt="XLSX";//set the default extension XLS
//SFD. Filter="Excel file (*. XLSX) | *. XLSX";//save files in the file type box when content

//if (SFD) ShowDialog ()==DialogResult. OK)//get the selected save file dialog box is
//{
//DoExport (enclosing textBox34, SFD FileName);

CreateExcelFile (path);//file address
ThreadStart entry=new ThreadStart (ex);//summation method is defined as a worker thread entry
WorkThread2=new Thread (entry);
WorkThread2. Start ();
//}

}
}



}
//the new excel
Private void CreateExcelFile (string FileName)
{
//the create
The object Nothing=System. Reflection. Missing. The Value;
Var app=new Excel. Application ();
App. The Visible=false;
Excel. The Workbook Workbook=app. Workbooks. The Add (Nothing);
Excel. The Worksheet Worksheet=(. Excel Worksheet) workBook. Sheets [1].



//worksheet. Columns. ColumnWidth=24;
Worksheet. Name="Work";
//headline
Worksheet. Cells [1, 1)="Serial number".
Worksheet. Cells [1, 2]="Time";
Worksheet. Cells [1, 3)="Status";
Worksheet. Cells [1, 4]="Vpv1 (V)";
Worksheet. Cells [1, 5)="vpv2 (V)";
Worksheet. Cells [1, 6]="Ppv1 (W)";
Worksheet. Cells [1, 7]="ppv2 (W)";
Worksheet. Cells [1, 8]="vBattery (V)";
Worksheet. Cells [1, 9]="Capacity (%)";
Worksheet. Cells [1, 10]="epvToday1 (kWh)";
Worksheet. Cells [1, 11]="epvTotal1 (kWh)";
Worksheet. Cells [1, 12]="epvToday2 (kWh)";
Worksheet. Cells [1, 13]="epvTotal2 (kWh)";
Worksheet. Cells [1, 14]="iChargePV1" (A);
Worksheet. Cells [1, 15]="iChargePV2" (A);
Worksheet. Cells [1] 16="outPutPower (VA)";
Worksheet. Cells [1, 17]="pAcCharge (W)";
Worksheet. Cells [18] 1,="vGrid (V)";
Worksheet. Cells [1, 19]="freqGrid (Hz)";
Worksheet. Cells [1, 20]="outPutVolt (V)";
Worksheet. Cells [1, 21]="freqOutPut (Hz)";
Worksheet. Cells [1, 22]="loadPercent (%)";
Worksheet. Cells [23] 1,="outPutCurrent" (A);
Worksheet. Cells [24] 1,="eacChargeToday (kWh)";
Worksheet. Cells [1, 25]="eacChargeTotal (kWh)";
Worksheet. Cells [1, 26]="eBatDisChargeToday (kWh)";
Worksheet. Cells [1, 27]="eBatDisChargeTotal (kWh)";
1, worksheet. Cells [28]="eacDisChargeToday (kWh)";
Worksheet. Cells [1, 29]="eacDisChargeTotal (kWh)";
//worksheet. Cells [1, 30]="activePower (W)";
//worksheet. Cells [1, 31]="ApparentPower (VA)";



Worksheet. SaveAs (FileName, the Missing of the Missing, the Missing of the Missing, the Missing, Excel. XlSaveAsAccessMode. XlNoChange, the Missing, the Missing, the Missing);

WorkBook. Close (false, the Missing, the Missing);
App. The Quit ();
}


//insert data
Private void ex ()
{
If (path!=null)
{
//while (true)
//{
If (serialPort1. IsOpen)
{

Excel. Application xApp=new Excel. Application ();
//1. Create Applicaton object

//xApp. Visible=true;

//2. Get the workbook object, you can use one of two ways: the following is open an existing file
//Excel Workbook xBook=xApp. Workbooks. Open (@ "E: \ \ 5 _ debugging software ThinkCamWorkstation \ PassData \ 192.168.116.248\2015-08-06 \ \ result 18 XLSX",
Excel. The Workbook xBook=xApp. Workbooks. Open (path,
Missing. The Value, the Missing Value, Missing. The Value of Missing. The Value,
Missing. The Value, the Missing Value, Missing. The Value of Missing. The Value,
Missing. The Value, the Missing Value, Missing. The Value of Missing. The Value);

//3. Specified to operate on Sheet
Excel. The Worksheet xSheet=(. Excel Worksheet) xBook. Sheets [1].
Int Count=xSheet. UsedRange. Rows. Count;//get lines
Int rowsCount=Count + 1;

writing//4.
Excel. Range firstColumn=xSheet. Get_Range (" A "+ rowsCount);//write to the location of the
FirstColumn. Value2=zhi;//write content
FirstColumn. EntireColumn. AutoFit ();//automatic adjust the column width

Excel. Range firstColumnB=xSheet. Get_Range (" B "+ rowsCount);
FirstColumnB. Value2=(DateTime. Now. The Year + "-" + DateTime. Now, the Month + "-" + DateTime. Now the Day + "" + DateTime. Now the Hour +" : "+ DateTime. Now. The Minute +" : "+ DateTime. Now. The Second). The ToString ();//time
FirstColumnB. EntireColumn. AutoFit ();

Excel. Range firstColumnC=xSheet. Get_Range (" C "+ rowsCount);
FirstColumnC. Value2=zhuangt;
FirstColumnC. EntireColumn. AutoFit ();

Excel. Range firstColumnD=xSheet. Get_Range (" D "+ rowsCount);
FirstColumnD. Value2=heVpv1;
FirstColumnD. EntireColumn. AutoFit ();

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related