0

I want to extract some data in the datatable, and want to send the email. But when I extract the data, excel has many blanks between the extracted data. The data which is not extracted make a blank row.

When I try to use RemoveRow() function, it doesn't work and still has a blank row.

    private void Email()
    {
        //get the data from database
        DataTable data = GetData();

        int maxLavel = Convert.ToInt32(data.Compute("max([AssignedID])", string.Empty));
        IWorkbook workbook;
        workbook = new HSSFWorkbook();

        for (int Emp = 0; Emp < maxLavel; Emp++)
        {
        ISheet sheet1 = workbook.CreateSheet("Sheet 1");
            int num = 0;

            //make a header row  
            IRow row1 = sheet1.CreateRow(0);
            for (int j = 0; j < data.Columns.Count; j++)
            {

                ICell cell = row1.CreateCell(j);

                String columnName = data.Columns[j].ToString();
                cell.SetCellValue(columnName);
            }
                //loops through data  
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    IRow row = sheet1.CreateRow(i + 1);

                    if (Emp == Convert.ToInt32(data.Rows[i][0]))
                    {
                        num++;
                        ICell cell = row.CreateCell(j);
                        for (int j = 0; j < data.Columns.Count; j++)
                        {

                            sheet1.AutoSizeColumn(j); //control cell width
                            String columnName = data.Columns[j].ToString();
                            cell.SetCellValue(data.Rows[i][columnName].ToString());
                        }
                    }

                    else ///here has problems
                    {
                            var row = sheet1.GetRow(i);
                            //sheet1.RemoveRow(row);
                            sheet1.ShiftRows(i + 1, sheet1.LastRowNum + 1, -1);
                    }

                }

                if (num != 0)
                {
                //send email
                }
        }
    }
Scarlett
  • 173
  • 12

2 Answers2

0

I'm not sure why you need to resend the same file again with different contents instead of simply sending a different file, but could you perhaps try moving the creation of your sheet inside the for loop, and try removing the entire sheet instead? E.g. something like this:

private void Email()
{
    //get the data from database
    DataTable data = GetData();

    int maxLavel = Convert.ToInt32(data.Compute("max([AssignedID])", string.Empty));
    IWorkbook workbook;
    workbook = new HSSFWorkbook();


    for (int Emp = 0; Emp < maxLavel; Emp++)
    {
        ISheet sheet1 = workbook.CreateSheet("Sheet 1");
        int num = 0;

        //make a header row  
        IRow row1 = sheet1.CreateRow(0);
        for (int j = 0; j < data.Columns.Count; j++)
        {

            ICell cell = row1.CreateCell(j);

            String columnName = data.Columns[j].ToString();
            cell.SetCellValue(columnName);
        }
            //loops through data  
            for (int i = 0; i < data.Rows.Count; i++)
            {
                IRow row = sheet1.CreateRow(i + 1);

                if (Emp == Convert.ToInt32(data.Rows[i][0]))
                {
                    num++;
                    for (int j = 0; j < data.Columns.Count; j++)
                    {
                        ICell cell = row.CreateCell(j);
                        sheet1.AutoSizeColumn(j); //control cell width
                        String columnName = data.Columns[j].ToString();
                        cell.SetCellValue(data.Rows[i][columnName].ToString());
                    }
                }

            }

            if (num != 0)
            {
            //send email
            }

        workbook.remove(sheet1);
    }
}
C. Helling
  • 1,394
  • 6
  • 20
  • 34
0

I figure it out..I don't need to remove the row.

IRow row = sheet1.CreateRow(i);

This should be changed.

      for (int i = 0; i < data.Rows.Count; i++)
            {
                if (Emp == Convert.ToInt32(data.Rows[i][0]))
                {
                    IRow row = sheet1.CreateRow(row_num);
                    num++; row_num++;
                    for (int j = 0; j < data.Columns.Count; j++)
                    {
                        ICell cell = row.CreateCell(j);
                        sheet1.AutoSizeColumn(j); //control cell width
                        String columnName = data.Columns[j].ToString();
                        cell.SetCellValue(data.Rows[i][columnName].ToString());
                    }
                }

            }
Scarlett
  • 173
  • 12