0

我正在尝试设置一个按钮,以便轻松地将我的应用程序中的数据库导出到 Excel 工作表。但是我在让它工作时遇到了问题。而且我无法弄清楚出了什么问题,因为我没有看到任何错误。

 {
            string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
            SqlConnection con = new SqlConnection(cs);
            con.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM AvSites", con);
            SqlDataAdapter dataadpter = new SqlDataAdapter(command);
            DataTable datatable = new DataTable("AvSites");
            dataadpter.Fill(datatable);
            datatable.WriteXml(Application.StartupPath + "AvSitesDbExport.xlsx");
            MessageBox.Show("export data");

        }

当我在调试模式下运行应用程序时,该按钮会在我的桌面上创建一个 xml (xlsx) 文件,我猜这还可以吗?由于它处于调试模式,因此不确定此处适用哪些规则。

但如果我发布应用程序,我看不到在任何地方创建的文件。

难道我做错了什么 ?Application.StartupPath 应该返回执行 .exe 的目录,对吗?

4

1 回答 1

0
 {
            string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
            SqlConnection con = new SqlConnection(cs);
            con.Open();
            SqlCommand command = new SqlCommand("SELECT * FROM AvSites", con);
            SqlDataAdapter dataadpter = new SqlDataAdapter(command);
            DataTable datatable = new DataTable("AvSites");
            WriteExcelWithNPOI(datatable,"xls")  /* "xls" or "xlsx"
            MessageBox.Show("export data");

        }
public void WriteExcelWithNPOI(DataTable dt, String extension)
      {           
          IWorkbook workbook;          

          if (extension == "xlsx") {
              workbook = new XSSFWorkbook();                
          }
          else if (extension == "xls")
          {
              workbook = new HSSFWorkbook();
          }
          else {
              throw new Exception("This format is not supported");
          }

          ISheet sheet1 = workbook.CreateSheet("Sheet 1");

          //make a header row
          IRow row1 = sheet1.CreateRow(0);

          for (int j = 0; j < dt.Columns.Count; j++)
          {

              ICell cell = row1.CreateCell(j);
              String columnName = dt.Columns[j].ToString();
              cell.SetCellValue(columnName);
          }

          //loops through data
          for (int i = 0; i < dt.Rows.Count; i++)
          {
              IRow row = sheet1.CreateRow(i + 1);
              for (int j = 0; j < dt.Columns.Count; j++)
              {

                  ICell cell = row.CreateCell(j);
                  String columnName = dt.Columns[j].ToString();
                  cell.SetCellValue(dt.Rows[i][columnName].ToString());
              }
          }

          using (var exportData = new MemoryStream())
          { 
              Response.Clear();                
              workbook.Write(exportData);
              if (extension == "xlsx") //xlsx file format
              {
                  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                  Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "ContactNPOI.xlsx"));                   
                  Response.BinaryWrite(exportData.ToArray());             
              }
              else if (extension == "xls")  //xls file format
              { 
                  Response.ContentType = "application/vnd.ms-excel";
                  Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "ContactNPOI.xls"));
                  Response.BinaryWrite(exportData.GetBuffer());
              }   
              Response.End();
          }
      }
于 2019-05-23T18:51:19.757 回答