1. 程式人生 > >datagridview資料匯入Excel

datagridview資料匯入Excel

#region 驗證可操作性
//申明儲存對話方塊
SaveFileDialog dlg = new SaveFileDialog();
//默然檔案字尾
dlg.DefaultExt = "xls ";
//檔案字尾列表
dlg.Filter = "EXCEL檔案(*.XLS)|*.xls ";
//默然路徑是系統當前路徑
dlg.InitialDirectory = Directory.GetCurrentDirectory();
//開啟儲存對話方塊
if (dlg.ShowDialog() == DialogResult.Cancel) return;
//返回檔案路徑
string fileNameString = dlg.FileName;
//驗證strFileName是否為空或值無效
if (fileNameString.Trim() == " ")
{ return; }
//定義表格內資料的行數和列數
int rowscount = dgv.Rows.Count;
int colscount = dgv.Columns.Count;
//行數必須大於0
if (rowscount <= 0)
{
MessageBox.Show("沒有資料可供儲存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//列數必須大於0
if (colscount <= 0)
{
MessageBox.Show("沒有資料可供儲存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//行數不可以大於65536
if (rowscount > 65536)
{
MessageBox.Show("資料記錄數太多(最多不能超過65536條),不能儲存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//列數不可以大於255
if (colscount > 255)
{
MessageBox.Show("資料記錄行數太多,不能儲存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//驗證以fileNameString命名的檔案是否存在,如果存在刪除它
FileInfo file = new FileInfo(fileNameString);
if (file.Exists)
{
try
{
file.Delete();
}
catch (Exception error)
{
MessageBox.Show(error.Message, "刪除失敗 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
#endregion
Excel.Application objExcel = null;
Excel.Workbook objWorkbook = null;
Excel.Worksheet objsheet = null;
try
{
//申明物件
objExcel = new Microsoft.Office.Interop.Excel.Application();
objWorkbook = objExcel.Workbooks.Add(Missing.Value);
objsheet = (Excel.Worksheet)objWorkbook.ActiveSheet;
//設定EXCEL不可見
objExcel.Visible = false;
//向Excel中寫入表格的表頭
int displayColumnsCount = 1;
for (int i = 0; i <= dgv.ColumnCount - 1; i )
{
if (dgv.Columns[i].Visible == true)
{
objExcel.Cells[1, displayColumnsCount] = dgv.Columns[i].HeaderText.Trim();
displayColumnsCount ;
}
}
//設定進度條
//tempProgressBar.Refresh();
//tempProgressBar.Visible = true;
//tempProgressBar.Minimum=1;
//tempProgressBar.Maximum=dgv.RowCount;
//tempProgressBar.Step=1;
//向Excel中逐行逐列寫入表格中的資料
for (int row = 0; row <= dgv.RowCount - 1; row )
{
//tempProgressBar.PerformStep();
displayColumnsCount = 1;
for (int col = 0; col < colscount; col )
{
if (dgv.Columns[col].Visible == true)
{
try
{
objExcel.Cells[row 2, displayColumnsCount] = dgv.Rows[row].Cells[col].Value.ToString().Trim();
displayColumnsCount ;
}
catch (Exception)
{
}
}
}
}
//隱藏進度條
//tempProgressBar.Visible = false;
//儲存檔案
objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
}
catch (Exception error)
{
MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
finally
{
//關閉Excel應用
if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
if (objExcel.Workbooks != null) objExcel.Workbooks.Close();
if (objExcel != null) objExcel.Quit();
objsheet = null;
objWorkbook = null;
objExcel = null;
}
MessageBox.Show(fileNameString "/n/n匯出完畢! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}