TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Deepanshu Mehta
NA
33
2.8k
how to load data tables in excel worksheets in a loop.
Sep 8 2017 1:36 AM
I am developing a DBF file comparison tool, one of the requirement is to get the comparison result in one excel file, but in different worksheets. i had used if-else loop, one to create the file load the first result table and save it at uuser defined location, Another loop is to open the file, adding another worksheet and and save the changes. but in the result file it shows the result from the first comparison loop, or with some modifications it start overriding the result.
can anyone please help me with it.
Excel._Application app =
new
Excel.Application();
Excel._Workbook workbook = app.Workbooks.Add(7);
if
(s == 0)
{
Excel._Worksheet worksheet = workbook.Worksheets.Add();
try
{ worksheet.Name = dt.TableName; }
catch
{
worksheet.Name =
"Extended"
+ dt.TableName;
}
worksheet.Cells[2, 1] =
"NAME"
;
worksheet.Cells[2, 2] =
"TAG"
;
worksheet.Cells[2, 3] =
"status"
;
worksheet.Cells[2, 4] =
"Reason"
;
for
(
int
k = 0; k < tableC.Rows.Count; k++)
{
for
(
int
l = 0; l < tableC.Columns.Count; l++)
{
worksheet.Cells[k + 3, l + 1] = tableC.Rows[k][l].ToString();
}
}
using
(SaveFileDialog save =
new
SaveFileDialog())
{
save.Filter =
"Excel Files(*.xls)|*.xlsx"
;
save.InitialDirectory = @
"C:\Users\User\Downloads\Report Files"
;
save.Title =
"Save Report File"
;
if
(save.ShowDialog() == DialogResult.OK)
{
workbook.SaveCopyAs(save.FileName);
workbook.Saved =
true
;
}
}
}
else
{ OpenFileDialog open =
new
OpenFileDialog();
open.Title =
"Select File"
;
open.InitialDirectory = @
"C:\Users\User\Downloads\Report Files"
;
open.Filter =
"Excel Files(*.xlsx)|*.xlsx"
;
open.Multiselect =
false
;
open.RestoreDirectory =
true
;
if
(open.ShowDialog() == DialogResult.OK)
{
Excel._Worksheet worksheet = workbook.Worksheets.Add();
//app.ActiveWorkbook.Sheets.Activate();
//Excel._Worksheet worksheet = workbook.Worksheets.Add();
//Excel._Worksheet worksheet = (Excel._Worksheet)workbook.ActiveSheet;
try
{ worksheet.Name = dt.TableName; }
catch
{ worksheet.Name =
"Extended"
+ dt.TableName; }
worksheet.Cells[2, 1] =
"NAME"
;
worksheet.Cells[2, 2] =
"TAG"
;
worksheet.Cells[2, 3] =
"status"
;
worksheet.Cells[2, 4] =
"Reason"
;
for
(
int
i = 0; i < tableC.Rows.Count; i++)
{
for
(
int
j = 0; j < tableC.Columns.Count; j++)
{
worksheet.Cells[i + 3, j + 1] = tableC.Rows[i][j].ToString();
}
}
workbook.Save();
workbook.Saved =
true
;
}
}
workbook.Close();
app.Quit();
Reply
Answers (
1
)
Facing the issue generating the data from MSSQL
Service show different behavior on client machine