关于C#的导出与导入
2021-03-30 05:28
标签:office 分享 object 无法 exception win apr soscw ring using System; using System.IO; namespace text_one private void button1_Click(object sender, EventArgs e) DataGridViewTextBoxColumn dtcTimeStamp = new DataGridViewTextBoxColumn(); } private void button2_Click(object sender, EventArgs e) { private void ExportExcels(string fileName, DataGridView myDGV) 关于C#的导出与导入 标签:office 分享 object 无法 exception win apr soscw ring 原文地址:https://www.cnblogs.com/nnguhx/p/9284111.html
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 Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
{
OpenFileDialog file = new OpenFileDialog();
file.Filter = "Exce文件(*,xls,xlsx)|*.xls;*.xlsx";
if (file.ShowDialog() == DialogResult.OK)
{
string FileName = file.FileName;
dataGridView1.DataSource = GetExcelData(FileName);//bang
dataGridView1.DataMember = "[sheet$]";
dtcTimeStamp.DataPropertyName = "TIMESTAMP";//SQL语句得到的列名,可从集合中获得
dtcTimeStamp.Width = 110;
dataGridView1.Columns.Add(dtcTimeStamp);//最后一定要添加进去
}
public static DataSet GetExcelData(string str)
{
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str + ";Extended Properties=‘Excel 12.0;HDR=YES;IMEX=1;‘";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [sheet$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[sheet$]");
myConn.Close();
return myDataSet;
}
string a ="";
ExportExcels(a, dataGridView1);
}
{
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") 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 {
worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
}
//写入数值
for (int r = 0; r {
for (int i = 0; i {
worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();//强行销毁
MessageBox.Show("文件xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}