版权声明: https://blog.csdn.net/Bibabu135766/article/details/79566675
一,读取Excel文件内容:
//根据excle的路径把第一个sheel中的内容放入datatable
public static DataTable ReadExcelToTable()//excel存放的路径
{
//连接字符串
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "E:/Peng.Tao/TEST/1.xlsx" + ";Extended Properties='Excel8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意
//string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字
string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字
string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串
//string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串
OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
DataSet set = new DataSet();
ada.Fill(set);
return set.Tables[0];
}
}
二,将数据导入至Excel:
protected void Excel1_Click(object sender, EventArgs e) {
//设置保存的文档类型
//Response.ContentType = "application/ms-word";//保存为Word类型
Response.ContentType = "application/ms-excel";//保存为Excel类型
//设置编码方式和内容保存的形式
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "Excel文档";
//设置保存为的文件名
string dateStr = DateTime.Now.ToString("yyyyMMddHHmmss");
string fileName = System.Web.HttpUtility.UrlEncode("料号文件" + dateStr, System.Text.Encoding.UTF8);
Response.AppendHeader("content-disposition", "attachment;filename=\"" + fileName + ".xls\"");
//开始导出;
this.EnableViewState = false;
StringWriter oStringWriter = new StringWriter();
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
oStringWriter = new StringWriter();
oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
//添加以下函数防止出现:“Gridview控件需在Runat=server中” 的异常
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
三,WinForm将DataGridView数据导出至EXCEL
private void button2_Click(object sender, EventArgs e)
{
ToExcel();
}
public void ToExcel()
{
try
{
//没有数据的话就不往下执行
if (dataGridView1.Rows.Count == 0)
return;
//实例化一个Excel.Application对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写
excel.Visible = true;
//新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
excel.Application.Workbooks.Add(true);
//生成Excel中列头名称
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (this.dataGridView1.Columns[i].Visible == true)
{
excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
}
}
//把DataGridView当前页的数据保存在Excel中
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
System.Windows.Forms.Application.DoEvents();
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (this.dataGridView1.Columns[j].Visible == true)
{
if (dataGridView1[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
}
}
}
}
//设置禁止弹出保存和覆盖的询问提示框
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
//保存工作簿
excel.Application.Workbooks.Add(true).Save();
//保存excel文件
excel.Save("E:\\Peng.Tao\\Winform\\MyFirstWinform" + "\\Data.xls");
//确保Excel进程关闭
excel.Quit();
excel = null;
GC.Collect();//如果不使用这条语句会导致excel进程无法正常退出,使用后正常退出
MessageBox.Show(this, "文件已经成功导出!", "信息提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示");
}
}