c# - Export to Excel holding file open, and not exporting first datagrid -
I am trying to export 2Datgrid views for excellence, but this data is final for both sets of grids. The row data is not the idea, I have checked the code and I can not see what I did wrong? Also after exporting it is creating a temporary file and then locking the file and does not exit gracefully and only the reboot allows you to delete the file? Any help would be great code below, for example if I save as test.xlsx, I get 2 files ~ $ test.xlsx and test.xlsx and both of the files are locked.
Private zero exprtbtn_Click (Object Sender, EventArgs e) {Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application (); Microsoft.Office.Interop.Excel._Workbook Workbook = app.Workbooks.Add (Type.Missing); Try / Try to create new Excelsheets in the workbook. Microsoft.Office.Interop.Excel._worksheet worksheet1 = null; Microsoft.Office.Interop.Excel._Worksheet Worksheet2 = Faucet; // Get reference to the first sheet by default, its name is Sheet1. // Worksheet SheetSheet 1 = Store the reference for Workbook. Sheets ["Sheet1"]; Worksheet 1 = Workbook. ActiveSite; // Rename the active sheet worksheet 1 Name = "switch totslets"; // For Excel in the archive header section (int i = 1; i & lt; switchtotalgrd.Column.Count + 1; i ++) {worksheet1.Cells [1, i] = Switchtland Ground. Column [i-1] .HeaderText; } // To store each row and column value for storage (int i = 0; i & lt; switchtotalgrd.rows.Count - 1; i ++) {for (int j = 0; j & lt; Switchtellelgrid.column.count;j ++) {Worksheet1.Cells [i + 2, j + 1] = switchtallgrounds Rao [i]. Sel [J]. Value Toasting (); }} // second worksheet int count = workbook. Adding Worksheets; Calculation; Excel Worksheet Added Sheet = Workbook. Worksheets Add (Type Missing, Workbook Worksheets [Calculate], Type. Missing, Type Missing); // Get reference to the first sheet by default, its name is Sheet1. // Worksheet Worksheet 2 = Store the reference of the workbook. Sheet ["sheet2"]; Worksheet 2 = Workbook. ActiveSite; // Rename the active sheet worksheet 2 Name = "itemized extension"; // For Excel in the archive header section (int i = 1; i & lt; fullcalt.coln calculation + 1; i ++) {worksheet 2. seals [1, ii] = fullerसेट. Column [i-1] .HeaderText; } // To store each row and column value for storage (int i = 0; i & lt; full-result. Ro. count1; i ++) {for (int j = 0; j & Lt; fullcalt.column; calculation; J ++) {If (fullresult.Rows [i] .seel [j] .value == faucet) {fullresult.Rows [i] .seell [ja] .value = "NA" ; } Worksheet 2 Seals [i + 2, j + 1] = fulcrum. Rao [i]. Sel [J]. Value. Toasting (); }} // save the application string file name = string.opt; SaveFileDialog saveFileDialog1 = new SaveFileDialog (); SaveFileDialog1.filter = "Excel Files | * .xls | All Files (*. *) | *. *"; SaveFileDialog1.FilterIndex = 2; SaveFileDialog1.RestoreDirectory = True; If (saveFileDialog1.ShowDialog () == DialogResult.OK) {fileName = saveFileDialog1.FileName; Workbook.SaveAs (filename, type.missing, type.missing, type.missing, type.missing, type.missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing , Type missing, type missing); } And return; } // Hold all the errors hold (System.Exception) {} Finally {workbook = null; App = null; }}
You might have to double check again to loop your external, For example (i = 0 int i; i & lt; fullresult.Rows.Count - 1; i ++) , may not have been compared to this and probably using par. should go?
Since you are using the Excel Com Interpol, you have to call this method to be able to issue the resource after you have done this.
A good practice, try {...} hold {...} finally {...} and finally release a comm Is for.
Comments
Post a Comment