导出到SQL时获得空行

本文关键字:SQL | 更新日期: 2023-09-27 18:14:26

每当我从excel上传文件并导出到SQL时,我都会得到多个空行( 

下面是我的代码。你能告诉我如何避免这个空行吗?

private void BindGrid()
        {
        String Constr = "Data Source=INNN;integrated security=true;InitialCatalog=info";
        SqlConnection con = new SqlConnection(Constr);
        con.Open();
        String SelectQuery = "Select ID,Name,Region,City from Infor";
        SqlCommand cmd = new SqlCommand(SelectQuery, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        con.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Download.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //To Export all pages
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        this.BindGrid();
        //GridView1.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
        {
            cell.BackColor = GridView1.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            //row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
            {
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = GridView1.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
            }
        }
        GridView1.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}

你能告诉我怎样才能避免这些空行吗?

private void ExportRowsToDB()
    {
        //container for source data
        DataTable dtProducts = new DataTable("Infor");
        //dtProducts.Columns.Add("ID");
        dtProducts.Columns.Add("Name");
        dtProducts.Columns.Add("Region");
        dtProducts.Columns.Add("City");
        DataRow dr = null;
        //iterate through each grid and populate source data
        foreach (GridViewRow gridRow in gvExcelFile.Rows)
        {
            dr = dtProducts.NewRow();
            //dr["ID"] = (gridRow.Cells[1].Text);
            dr["Name"] = gridRow.Cells[1].Text;
            dr["Region"] = gridRow.Cells[2].Text;
            dr["City"] = gridRow.Cells[3].Text;
            dtProducts.Rows.Add(dr);
        }
        //insert data into destination table
        CopyData(dtProducts);
    }
    public void CopyData(DataTable sourceData)
    {
        string destConnString =
            ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        // Set up the bulk copy object.
        using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(destConnString))
        {
            bulkCopy.DestinationTableName = "dbo.Infor";
            // Guarantee that columns are mapped correctly by
            // defining the column mappings for the order.    
            //bulkCopy.ColumnMappings.Add("ID", "ID");
            bulkCopy.ColumnMappings.Add("Name", "Name");
            bulkCopy.ColumnMappings.Add("Region", "Region");
            bulkCopy.ColumnMappings.Add("City", "City");
            // Write from the source to the destination.
            bulkCopy.WriteToServer(sourceData);
        }
    }

下面是我的sql表的细节

Table Infor(
[ID] [int] IDENTITY(1,1) NOT NULL,
FormattedID AS ('CUID' + RIGHT('00' + CAST(ID AS VARCHAR(10)),10)),
[Name] [nvarchar](20) NOT NULL,
[Region] [nvarchar](20) NULL,
[City] [nvarchar](20) NULL,
FOREIGN KEY (ID) REFERENCES RequestInf (ID)

导出到SQL时获得空行

为第一列的长度添加一个"where"

 String SelectQuery = "Select ID,Name,Region,City from Infor WHERE Len(F1) > 0";
相关文章:
  • 没有找到相关文章