62,266
社区成员
发帖
与我相关
我的任务
分享
//导入到Excel
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
IWorksheet sheet1 = workbook.Worksheets[0];
sheet1.ImportDataTable(datasource, isfieldnameshown, firstrow, firstcolumn);
sheet1.SaveAs("test.xls", ExcelSaveType.SaveAsXLS, HttpContext.Current.Response, ExcelDownloadType.PromptDialog);
//导出Excel
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = excelEngine.Excel.Workbooks.Open(fuExcel.PostedFile.FileName);
IWorksheet sheet1 = workbook.Worksheets[1];
//取出Sheet中的数据放入DataTable
DataTable dt1 = sheet1.ExportDataTable(sheet1.Range, ExcelExportDataTableOptions.None);
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;//设置http输出流的格式,必须有,否则导出乱码
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("filename.xls").ToString());
//Response.ContentType = "Application/ms-excel";
// this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string n = ConfigurationManager.AppSettings["con"].ToString() + System.Web.HttpContext.Current.Server.MapPath("") + ConfigurationManager.AppSettings["dbPath"] + ";";
OleDbConnection connect = new OleDbConnection(n);
connect.Open();
//string que = "select * from renkou order by id desc";
OleDbDataAdapter da = new OleDbDataAdapter(que, connect);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
// GridView1.AllowPaging = false;
// GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
int num = GridView1.Rows.Count;//获得行数
for (int i = 0; i < num; i++)
{
CheckBox ch = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
ch.Visible = false;
}
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
private void DaoRu()
{
OpenFileDialog myDiag = new OpenFileDialog();
myDiag.Filter = "Excel文件(*.xls)|*.xls";
string myFile = myDiag.FileName;
if (myDiag.ShowDialog() == DialogResult.OK)
{
OleDbConnection con = new OleDbConnection("server=.;uid=sa;pwd=sa;database=MyOffice;max pool size=30");
con.Open();
string sql = "insert into Execl(id,name) select * from "+
"OpenDataSource( 'Microsoft.Jet.OLEDB.4.0 ', 'Data Source= " + myDiag.FileName + ";Extended properties=Excel 8.0 ')...[Book1$]";
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
}
}