在数据集列中获取特定数据并在 excel 中导出
Getting Specific Data in DataSet Column and export in excel
我的数据集中有 5 列。
姓名、年龄、性别、地址和联系电话
然后我在数据集中有 3 个数据行
在 excel 中导出时我只需要姓名、地址和联系电话。.
我怎样才能做到这一点?
我正在使用 Excel = Microsoft.Office.Interop.Excel;
我只得到第一行..不是数据集中的所有行
这是我获取值的代码
Microsoft.Office.Interop.Excel.Application ExcelAp = new Microsoft.Office.Interop.Excel.Application();
ExcelAp.Application.Workbooks.Add(Type.Missing);
ExcelAp.Columns.ColumnWidth = 25;
int i =0;
foreach( DataRow datarow in DsNow.Tables[0].Rows)
{
for(i = 0; DsNow.Tables[0].Rows.Count - 1; i ++)
{
ExcelAp.Cells[i + 6, 1] = datarow["name"].ToString();
ExcelAp.Cells[i + 6, 2] =datarow["address"].ToString();
ExcelAp.Cells[i + 6, 3] =datarow["contactnumber"].ToString();
}
}
请帮忙
当只用一个循环就可以完成时,为什么要同时使用 FOREACH 和 FOR 循环。检查以下代码,稍作修改:
int i =0;
for(i = 0; DsNow.Tables[0].Rows.Count - 1; i ++)
{
ExcelAp.Cells[i + 6, 1] = DsNow.Tables[0].Rows[i]["name"].ToString();
ExcelAp.Cells[i + 6, 2] = DsNow.Tables[0].Rows[i]["address"].ToString();
ExcelAp.Cells[i + 6, 3] = DsNow.Tables[0].Rows[i]["contactnumber"].ToString();
}
Public void Excel()
{
Microsoft.Office.Interop.Excel.Application app;
Workbook workbook = null;
Worksheet worksheet = null;
Range range = null;
int i,j,k=1;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
workbook = app.Workbooks.Add(1);
worksheet = (Worksheet)workbook.Sheets[1];
worksheet.Name = "Sheet1";
worksheet.Cells.Locked = false;
//Column1
j = 1;
range = worksheet.Rows.get_Range("A" + j.ToString(), "A" + j.ToString());
range.Font.Size = 11;
((Range)worksheet.Cells[j, 1]).EntireColumn.ColumnWidth = 15;
range.Font.Bold = true;
range.MergeCells = true;
range.HorizontalAlignment = Constants.xlCenter;
//Content
worksheet.Cells[j, 1] = "Name";
//Column2
range = worksheet.Rows.get_Range("B" + j.ToString(), "B" + j.ToString());
range.Font.Size = 11;
((Range)worksheet.Cells[j, 2]).EntireColumn.ColumnWidth = 15;
range.Font.Bold = true;
range.MergeCells = true;
range.HorizontalAlignment = Constants.xlCenter;
//Content
worksheet.Cells[j, 2] = "Address";
//Column3
range = worksheet.Rows.get_Range("C" + j.ToString(), "C" + j.ToString());
range.Font.Size = 11;
((Range)worksheet.Cells[j, 3]).EntireColumn.ColumnWidth = 15;
range.Font.Bold = true;
range.MergeCells = true;
range.HorizontalAlignment = Constants.xlCenter;
//Content
worksheet.Cells[j, 3] = "Contact Number";
//Loop
DataTable dt = new DataTable();
for (i = 0; i < dt.Rows.Count; i++)
{
//Column1
range = worksheet.Rows.get_Range("A" + k.ToString(), "A" + k.ToString());
worksheet.Cells[k, 1] = dt.Rows[i]["NAME"].ToString();
//Column2
range = worksheet.Rows.get_Range("B" + k.ToString(), "B" + k.ToString());
worksheet.Cells[k, 2] = dt.Rows[i]["ADDRESS"].ToString();
//Column3
range = worksheet.Rows.get_Range("C" + k.ToString(), "C" + k.ToString());
worksheet.Cells[k, 3] = dt.Rows[i]["CONTACT_NUMBER"].ToString();
k = k +1;
}
}
我的数据集中有 5 列。 姓名、年龄、性别、地址和联系电话 然后我在数据集中有 3 个数据行
在 excel 中导出时我只需要姓名、地址和联系电话。. 我怎样才能做到这一点? 我正在使用 Excel = Microsoft.Office.Interop.Excel; 我只得到第一行..不是数据集中的所有行
这是我获取值的代码
Microsoft.Office.Interop.Excel.Application ExcelAp = new Microsoft.Office.Interop.Excel.Application();
ExcelAp.Application.Workbooks.Add(Type.Missing);
ExcelAp.Columns.ColumnWidth = 25;
int i =0;
foreach( DataRow datarow in DsNow.Tables[0].Rows)
{
for(i = 0; DsNow.Tables[0].Rows.Count - 1; i ++)
{
ExcelAp.Cells[i + 6, 1] = datarow["name"].ToString();
ExcelAp.Cells[i + 6, 2] =datarow["address"].ToString();
ExcelAp.Cells[i + 6, 3] =datarow["contactnumber"].ToString();
}
}
请帮忙
当只用一个循环就可以完成时,为什么要同时使用 FOREACH 和 FOR 循环。检查以下代码,稍作修改:
int i =0;
for(i = 0; DsNow.Tables[0].Rows.Count - 1; i ++)
{
ExcelAp.Cells[i + 6, 1] = DsNow.Tables[0].Rows[i]["name"].ToString();
ExcelAp.Cells[i + 6, 2] = DsNow.Tables[0].Rows[i]["address"].ToString();
ExcelAp.Cells[i + 6, 3] = DsNow.Tables[0].Rows[i]["contactnumber"].ToString();
}
Public void Excel()
{
Microsoft.Office.Interop.Excel.Application app;
Workbook workbook = null;
Worksheet worksheet = null;
Range range = null;
int i,j,k=1;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
workbook = app.Workbooks.Add(1);
worksheet = (Worksheet)workbook.Sheets[1];
worksheet.Name = "Sheet1";
worksheet.Cells.Locked = false;
//Column1
j = 1;
range = worksheet.Rows.get_Range("A" + j.ToString(), "A" + j.ToString());
range.Font.Size = 11;
((Range)worksheet.Cells[j, 1]).EntireColumn.ColumnWidth = 15;
range.Font.Bold = true;
range.MergeCells = true;
range.HorizontalAlignment = Constants.xlCenter;
//Content
worksheet.Cells[j, 1] = "Name";
//Column2
range = worksheet.Rows.get_Range("B" + j.ToString(), "B" + j.ToString());
range.Font.Size = 11;
((Range)worksheet.Cells[j, 2]).EntireColumn.ColumnWidth = 15;
range.Font.Bold = true;
range.MergeCells = true;
range.HorizontalAlignment = Constants.xlCenter;
//Content
worksheet.Cells[j, 2] = "Address";
//Column3
range = worksheet.Rows.get_Range("C" + j.ToString(), "C" + j.ToString());
range.Font.Size = 11;
((Range)worksheet.Cells[j, 3]).EntireColumn.ColumnWidth = 15;
range.Font.Bold = true;
range.MergeCells = true;
range.HorizontalAlignment = Constants.xlCenter;
//Content
worksheet.Cells[j, 3] = "Contact Number";
//Loop
DataTable dt = new DataTable();
for (i = 0; i < dt.Rows.Count; i++)
{
//Column1
range = worksheet.Rows.get_Range("A" + k.ToString(), "A" + k.ToString());
worksheet.Cells[k, 1] = dt.Rows[i]["NAME"].ToString();
//Column2
range = worksheet.Rows.get_Range("B" + k.ToString(), "B" + k.ToString());
worksheet.Cells[k, 2] = dt.Rows[i]["ADDRESS"].ToString();
//Column3
range = worksheet.Rows.get_Range("C" + k.ToString(), "C" + k.ToString());
worksheet.Cells[k, 3] = dt.Rows[i]["CONTACT_NUMBER"].ToString();
k = k +1;
}
}