Member 12742983 Ответов: 1

Создание имен для листа excel в книге excel


Привет.....
у меня есть одно приложение, преобразующее несколько файлов. dbf в один лист excel,мой pblm-это при создании листа книги excelwork сохранить как Table1, Table2,..... Table6, но я хочу имя файла dbf в качестве имени листа

Что я уже пробовал:

protected void Button1_Click(object sender, EventArgs e)
{
  string Text = "";
  int i = 0;
  
  if (FileUpload1.HasFiles)
  {
    DataSet ds = new DataSet();
    
    foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
    {
      uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/"), FileUpload1.FileName));

      Text += String.Format("{0}<br>", uploadedFile.FileName);
      string dbfFileName = Server.MapPath(FileUpload1.FileName);// file name with path.
      // fileName = FileUpload1.FileName;// Only file name.
      //string dbfFileName = @"D:\myData.dbf";
      string constr = "Provider=VFPOLEDB.1;Data Source=" + dbfFileName;
      string ExcelFileName = AppDomain.CurrentDomain.BaseDirectory + "converted_file.xls";
      using (OleDbConnection con = new OleDbConnection(constr))
      {
        var sql = "select * from " + Path.GetFileName(dbfFileName) + ";";
        OleDbCommand cmd = new OleDbCommand(sql, con);
        DataTable dt = new DataTable();
                    
        try
        {
          con.Open();
        }
        catch (Exception ex)
        {
          // Console.WriteLine("Error connecting database: " + ex.Message);
          return;
        }
        if (con.State == ConnectionState.Open)
        {
          OleDbDataAdapter da = new OleDbDataAdapter(cmd);
          //  Console.Write("Reading database...  ");
          da.Fill(dt);
          ds.Tables.Add(dt);
                       
          // Console.WriteLine("Completed.");
        }
        if (con.State == ConnectionState.Open)
        {
          try
          {
            con.Close();
          }
          catch
          {
          }
        }
      }
    }
    Response.Write(Text);
    using (XLWorkbook wb = new XLWorkbook())
    {
      foreach (DataTable dt in ds.Tables)
      {
        //Add DataTable as Worksheet.
        wb.Worksheets.Add(dt);
      }

      //Export the Excel file.
      Response.Clear();
      Response.Buffer = true;
      Response.Charset = "";
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      string FileName = "dbffiles" + DateTime.Now + ".xls";
      // Response.AddHeader("content-disposition", "attachment;filename"= + FileName);
      Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
      using (MemoryStream MyMemoryStream = new MemoryStream())
      {
        wb.SaveAs(MyMemoryStream);
                  
        MyMemoryStream.WriteTo(Response.OutputStream);
        Response.Flush();
        Response.End();
      }
    }
  }
  else
  {
    Text = "";
  }
}

public override void VerifyRenderingInServerForm(Control control)
{
}

1 Ответов

Рейтинг:
0

Karthik_Mahalingam

пробовать

OleDbCommand cmd = new OleDbCommand(sql, con);
          DataTable dt = new DataTable();
          dt.TableName = dbfFileName;