導(dǎo)出datagridview到excel

首先using

using Excel = Microsoft.Office.Interop.Excel;


```

//導(dǎo)出excel

? ? ? ? private void button5_Click(object sender, EventArgs e)

? ? ? ? {

? ? ? ? ? ? string fileName = "";

? ? ? ? ? ? string saveFileName = "";

? ? ? ? ? ? SaveFileDialog saveDialog = new SaveFileDialog();

? ? ? ? ? ? saveDialog.DefaultExt = "xlsx";

? ? ? ? ? ? saveDialog.Filter = "Excel文件|*.xlsx";

? ? ? ? ? ? saveDialog.FileName = fileName;

? ? ? ? ? ? saveDialog.ShowDialog();

? ? ? ? ? ? saveFileName = saveDialog.FileName;

? ? ? ? ? ? if (saveFileName.IndexOf(":") < 0) return; //被點(diǎn)了取消

? ? ? ? ? ? Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

? ? ? ? ? ? if (xlApp == null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? MessageBox.Show("無(wú)法創(chuàng)建Excel對(duì)象,您的電腦可能未安裝Excel");

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;

? ? ? ? ? ? Microsoft.Office.Interop.Excel.Workbook workbook =

? ? ? ? ? ? ? ? ? ? ? ? workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

? ? ? ? ? ? Microsoft.Office.Interop.Excel.Worksheet worksheet =

? ? ? ? ? ? ? ? ? ? ? ? (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //寫(xiě)入標(biāo)題? ? ? ? ? ?

? ? ? ? ? ? for (int i = 0; i < dataGridView1.ColumnCount; i++)

? ? ? ? ? ? { worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }

? ? ? ? ? ? //寫(xiě)入數(shù)值

? ? ? ? ? ? for (int r = 0; r < dataGridView1.Rows.Count; r++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? for (int i = 0; i < dataGridView1.ColumnCount; i++)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? System.Windows.Forms.Application.DoEvents();

? ? ? ? ? ? }

? ? ? ? ? ? worksheet.Columns.EntireColumn.AutoFit();//列寬自適應(yīng)

? ? ? ? ? ? MessageBox.Show(fileName + "資料保存成功", "提示", MessageBoxButtons.OK);

? ? ? ? ? ? if (saveFileName != "")

? ? ? ? ? ? {

? ? ? ? ? ? ? ? try

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? workbook.Saved = true;

? ? ? ? ? ? ? ? ? ? workbook.SaveCopyAs(saveFileName);? //fileSaved = true;? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch (Exception ex)

? ? ? ? ? ? ? ? {//fileSaved = false;? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? MessageBox.Show("導(dǎo)出文件時(shí)出錯(cuò),文件可能正被打開(kāi)!\n" + ex.Message);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? xlApp.Quit();

? ? ? ? ? ? GC.Collect();//強(qiáng)行銷(xiāo)毀? ? ?

? ? ? ? }

```


下載地址:

Microsoft.Office.Interop.Excel

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容