使用 DataGridView 控件,可以顯示和編輯來自多種不同類型的數據源的表格數據。也可以導出.txt,.xls等格式的文件。今天我們就先介紹一下用DataGridView把導入txt文件,導出xls文件。。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop.Excel;
之所以先把引用的命名空間單獨列出來是因為樓主為了添加最后一個引用也查了許多資料。如果只是這樣簡單的引用最后一個命運空間,編譯時出現“命名空間“Microsoft”中不存在類型或命名空間名稱“Office”(是否缺少程序集引用?) ”的尷尬錯誤。解決方案就是添加引用和命名空間 ,添加Microsoft.Office.Interop.Excel引用,它的默認路徑是C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll。。。
接下來就是想DataGridView導入文件
private void button1_Click(object sender, EventArgs e)
{
System.Data.DataTable dt = new System.Data.DataTable();
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
//記錄文件路徑
textBox1.Text = fileName;
//按特定編碼方式將文件讀入流,如果導入出現亂碼可以更改這里的編碼方式
StreamReader sr = new StreamReader(fileName, Encoding.UTF8);
string strLine = sr.ReadLine(); //讀取一行
//文件以Tab分開
string[] ColNmae = strLine.Split('\t');
for (int i = 0; i < ColNmae.Length; i++)
{
//dt.Columns.Add(ColNmae[i]);
dt.Columns.Add();
dt.Columns[i].ColumnName = ColNmae[i];
}
while (true)
{
strLine = sr.ReadLine();
if (string.IsNullOrEmpty(strLine) == true)
{
break;
}
else
{
DataRow dr = dt.NewRow();
string[] strList = strLine.Split('\t');
for (int i = 0; i < strList.Length; i++)
{
dr[ColNmae[i]] = strList[i];
}
dt.Rows.Add(dr);
}
}
sr.Close();
dataGridView1.DataSource = dt;
}
}
出現亂碼可以更改文件的編碼方式
接下來就是導出為excel文件
private void button2_Click(object sender, EventArgs e)
{
string fileName = ""; //文件路徑,文件名
saveFileDialog1.DefaultExt = "xls";//獲取或設置默認文件擴展名
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = saveFileDialog1.FileName;
}
else
{
return;
}
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("無法創建Excel對象,您的電腦可能未安裝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
//寫入標題
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
}
//寫入數值
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();//列寬自適應
MessageBox.Show(fileName + "的簡明資料保存成功", "提示", MessageBoxButtons.OK);
if (fileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(fileName);
//fileSaved = true;
}
catch (Exception ex)
{
//fileSaved = false;
MessageBox.Show("導出文件時出錯,文件可能正被打開!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();//強行銷毀
}