110,571
社区成员
发帖
与我相关
我的任务
分享
protected void Button1_Click(object sender, EventArgs e)
{
string XX = Label1.Text + Label3.Text;
Export("application/ms-excel", XX + "月份采购计划.xls");
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public void CreateExcel(DataTable dt, string FileName)
{
try
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
//DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加\n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + ",";
}
}
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
int fees = 0;
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加\n
{
ls_item += row[i].ToString().Trim() + "\n";// row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString().Trim() + ",";
}
}
resp.Write(ls_item);
ls_item = "";
}
resp.End();
}
catch (Exception ex)
{
// Response.Write("<script>alert('"+ex .Data +ex.Message +ex.Source +ex.TargetSite+"');history.go(-1);</script>");
throw;
}
}
这个是CSV的-,-/// <summary>
/// 将指定的Dataset导出到Excel文件
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public int Export(string path, System.Data.DataTable dt)
{
try
{
Microsoft.Office.Interop.Excel.Application excelkccx = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook wb = excelkccx.Workbooks.Add(true);
Range rngA = (Range)excelkccx.Columns["A", Type.Missing];//设置单元格格式
rngA.NumberFormatLocal = "@";//字符型格式
//Range rngD = (Range)excelkccx.Columns["D", Type.Missing];//设置单元格格式
//rngD.NumberFormatLocal = "0.00";
//Range rngE = (Range)excelkccx.Columns["E", Type.Missing];//设置单元格格式
//rngE.NumberFormatLocal = "0.00";
//Range rngF = (Range)excelkccx.Columns["F", Type.Missing];//设置单元格格式
//rngF.NumberFormatLocal = "0.00";
//Range rngAll = (Range)excelkccx.Columns["B","C"];
//rngAll.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //字段水平居中
//Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)excelkccx.Workbooks[1].Worksheets[1];
//string name = ws.Name; //获取sheet名称
//System.Data.DataTable dt = ds.Tables[0];
int row = 2;
for (int i = 0; i < dt.Columns.Count; i++)
{
excelkccx.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
excelkccx.Cells[row, j + 1] = dt.Rows[i][j].ToString();
}
row++;
}
wb.SaveAs(path);
wb.Close(false, null, null);
excelkccx.Quit();
wb = null;
excelkccx = null;
return 1;
}
catch(Exception ex)
{
return 0;
}
}
这个方法你直接拿去可以用 我一直在用的 不谢哈 string strCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=t_alibaba_data.xlsx;Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = "SELECT * FROM [t_alibaba_data$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
ds = new DataSet();
myCommand.Fill(ds);
上面从t_alibaba_data.xlsx文件中读取数据到Dataset类型的ds中