private void DataGridViewToExcel() { bool flag = false; if (this._GrdData != null) { Exception exception2; this.dBegin = DateTime.Now; if (this.appExcel == null) { this.appExcel = new ApplicationClass(); } this.wkBook = this.appExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); this.wkSheet = (Worksheet)this.wkBook.Worksheets[1]; this.appExcel.Visible = false; this.dEnd = DateTime.Now; this.nCols = this._GrdData.ColumnCount; this.nRows = this._GrdData.Rows.Count; this.nCurrR = 1; this.nCurrC = 1; if (this._RptTitle != "") { this.SetControlText(this.lblState, "正在处理报表头......"); this.wkSheet.Cells[this.nCurrR, this.nCurrC] = this._RptTitle; this.range = this.wkSheet.get_Range(this.wkSheet.Cells[this.nCurrR, 1], this.wkSheet.Cells[this.nCurrR, this.nCols]); this.range.MergeCells = true; this.range.Font.Bold = true; this.range.Font.Size = 0x12; this.range.Font.Name = "宋体"; this.range.HorizontalAlignment = XlHAlign.xlHAlignCenter; this.range.VerticalAlignment = XlVAlign.xlVAlignCenter; this.range.EntireRow.AutoFit(); this.nRows++; this.nCurrR++; } this.nRows++; this.SetProgressMaxMin(this.prgMain, this.nRows, 0); this.SetProgressValue(this.prgMain, this.nCurrR); this.SetControlText(this.lblState, "正在处理列头......"); this.nCurrC = 1; while (this.nCurrC <= this.nCols) { this.wkSheet.Cells[this.nCurrR, this.nCurrC] = this._GrdData.Columns[this.nCurrC - 1].HeaderText; this.nCurrC++; } this.nCurrR++; try { foreach (DataGridViewRow row in (IEnumerable)this._GrdData.Rows) { this.nCurrC = 0; while (this.nCurrC < this.nCols) { Exception exception; string str3; if (row.Cells[this.nCurrC].Value != null) { try { string str2 = row.Cells[this.nCurrC].Value.ToString().Trim(); double a; if (str2.StartsWith("=") || double.TryParse(str2, out a)) { str2 = "'" + str2; } this.wkSheet.Cells[this.nCurrR, this.nCurrC + 1] = str2; } catch (Exception exception1) { exception = exception1; str3 = this.wkSheet.Cells[this.nCurrR, this.nCurrC + 1].ToString(); string str4 = row.Cells[this.nCurrC].Value.ToString(); MessageBox.Show(string.Format("在:nCurrC=[{3}] nCurrR=[{4}]==[{0}]=:=[{1}]=:=[{2}] nCols=[{5}]", new object[] { str3, exception.Message, str4, this.nCurrC, this.nCurrR, this.nCols })); } } else { try { this.wkSheet.Cells[this.nCurrR, this.nCurrC + 1] = ""; } catch (Exception exception3) { exception = exception3; str3 = this.wkSheet.Cells[this.nCurrR, this.nCurrC + 1].ToString(); } } this.nCurrC++; } this.SetControlText(this.lblState, "正在处理数据......(" + this.nCurrR.ToString() + "/" + this.nRows.ToString() + ")"); this.SetProgressValue(this.prgMain, this.nCurrR); this.nCurrR++; } } catch (Exception exception4) { exception2 = exception4; MessageBox.Show(exception2.Source + "\r\n" + exception2.Message + exception2.StackTrace + "\r\n" + exception2.TargetSite.ToString()); } this.range = null; this.range = this.wkSheet.get_Range(this.wkSheet.Cells[1, 1], this.wkSheet.Cells[this.nRows, this.nCols]); this.range.Cells.Borders.LineStyle = XlLineStyle.xlContinuous; this.range.Borders.Weight = XlBorderWeight.xlThin; this.range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlMedium; this.range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlMedium; this.range.Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlMedium; this.range.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlMedium; if (this._FileName.Trim() == "") { this._FileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "datagrid.xls"); } if (this._FileName.ToLower().IndexOf(".xls") <= 0) { this._FileName = this._FileName + ".xls"; } if (File.Exists(this._FileName)) { try { File.Delete(this._FileName); } catch (Exception exception5) { exception2 = exception5; MessageBox.Show(exception2.Message); } } try { this.wkBook.SaveAs(this._FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); flag = true; } catch (Exception exception6) { exception2 = exception6; flag = false; MessageBox.Show(exception2.Message); } try { this.appExcel.Workbooks.Close(); this.appExcel.Quit(); DataIE.KillProgress((IntPtr)this.appExcel.Hwnd); this.appExcel = null; } catch (Exception exception7) { exception2 = exception7; flag = false; MessageBox.Show(exception2.Message); } if (flag) { MessageBox.Show("已经成功导出:" + this._FileName); } base.Close(); } }
private void DataTableToExcel() { if (this._TbData != null) { this.dBegin = DateTime.Now; if (this.appExcel == null) { this.appExcel = new ApplicationClass(); } this.wkBook = this.appExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); this.wkSheet = (Worksheet)this.wkBook.Worksheets[1]; this.appExcel.Visible = false; this.dEnd = DateTime.Now; this.nCols = this._TbData.Columns.Count; this.nRows = this._TbData.Rows.Count; this.nCurrR = 1; this.nCurrC = 1; if (this._RptTitle != "") { this.SetControlText(this.lblState, "正在处理表头......"); this.wkSheet.Cells[this.nCurrR, this.nCurrC] = this._RptTitle; this.range = this.wkSheet.get_Range(this.wkSheet.Cells[this.nCurrR, 1], this.wkSheet.Cells[this.nCurrR, this.nCols]); this.range.MergeCells = true; this.range.Font.Bold = true; this.range.Font.Size = 0x12; this.range.Font.Name = "宋体"; this.range.HorizontalAlignment = XlHAlign.xlHAlignCenter; this.range.VerticalAlignment = XlVAlign.xlVAlignCenter; this.range.EntireRow.AutoFit(); this.nRows++; this.nCurrR++; } this.nRows++; this.SetProgressMaxMin(this.prgMain, this.nRows, 0); this.SetProgressValue(this.prgMain, this.nCurrR); this.SetControlText(this.lblState, "正在处理列头......"); this.nCurrC = 1; while (this.nCurrC <= this.nCols) { this.wkSheet.Cells[this.nCurrR, this.nCurrC] = this._TbData.Columns[this.nCurrC - 1].ColumnName; this.nCurrC++; } this.nCurrR++; this.SetProgressValue(this.prgMain, this.nCurrR); foreach (DataRow row in this._TbData.Rows) { this.nCurrC = 0; while (this.nCurrC < this.nCols) { if (row[this.nCurrC] != null) { string str = row[this.nCurrC].ToString().Trim(); double a; if (str.StartsWith("=") || double.TryParse(str, out a)) { str = "'" + str; } this.wkSheet.Cells[this.nCurrR, this.nCurrC + 1] = str; } else { this.wkSheet.Cells[this.nCurrR, this.nCurrC + 1] = ""; } this.nCurrC++; } this.SetControlText(this.lblState, "正在处理数据......(" + this.nCurrR.ToString() + "/" + this.nRows.ToString() + ")"); this.SetProgressValue(this.prgMain, this.nCurrR); this.nCurrR++; } this.range = null; this.range = this.wkSheet.get_Range(this.wkSheet.Cells[1, 1], this.wkSheet.Cells[this.nRows, this.nCols]); this.range.Cells.Borders.LineStyle = XlLineStyle.xlContinuous; this.range.Borders.Weight = XlBorderWeight.xlThin; this.range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlMedium; this.range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlMedium; this.range.Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlMedium; this.range.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlMedium; if (this._FileName.Trim() == "") { this._FileName = Path.Combine(System.Windows.Forms.Application.StartupPath, this._TbData.TableName + ".xls"); } if (this._FileName.ToLower().IndexOf(".xls") <= 0) { this._FileName = this._FileName + ".xls"; } this.wkBook.SaveAs(this._FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); this.appExcel.Workbooks.Close(); this.appExcel.Quit(); DataIE.KillProgress((IntPtr)this.appExcel.Hwnd); this.appExcel = null; MessageBox.Show("已经成功导出:" + this._FileName); base.Close(); } }