c#怎么连接excel 读取某列怎么实现
3个回答
展开全部
//这是我开发过程中的一个例子,通过集合与excel接口实现的
private DataSet ListToDataSet(ObservableCollection< CClientInfo> iExportCorrectList)
{
DataSet iDataSet = new DataSet();
DataTable iDataTable = new DataTable();
iDataTable.Columns.Add(" 客户名称" , typeof(string));
iDataTable.Columns.Add(" 性别" , typeof(string));
iDataTable.Columns.Add(" 联系电话" , typeof(string));
iDataTable.Columns.Add(" 邮箱" , typeof(string));
int iRowCount = iClientInfoList.Count;
for (int iCount = 0; iCount < iRowCount; iCount++)
{
DataRow iDataRow = iDataTable.NewRow();
iDataRow[0] = iClientInfoList[iCount].iName;
iDataRow[1] = iClientInfoList[iCount].iSex;
iDataRow[2] = iClientInfoList[iCount].iTel;
iDataRow[3] = iClientInfoList[iCount].iEmail;
iDataRow[4] = iClientInfoList[iCount].iQQ;
iDataRow[5] = iClientInfoList[iCount].iCompany;
iDataRow[6] = iClientInfoList[iCount].iAddress;
iDataTable.Rows.Add(iDataRow);
}
iDataSet.Tables.Add(iDataTable);
return iDataSet;
}
private bool DataSetToExcel(DataSet iExportDataSet, string iExportFilePath)
{
try
{
DataTable iDataTable = iExportDataSet.Tables[0];
StreamWriter sw = new StreamWriter(iExportFilePath, false, Encoding.GetEncoding(" gb2312"));
StringBuilder sb = new StringBuilder();
for (int ColumnIndex = 0; ColumnIndex < iDataTable.Columns.Count; ColumnIndex++)
{
sb.Append(iDataTable.Columns[ColumnIndex].ColumnName.ToString() + " \t");
}
sb.Append(Environment.NewLine);
for (int RowIndex = 0; RowIndex < iDataTable.Rows.Count; RowIndex++)
{
for (int ColumnIndex = 0; ColumnIndex < iDataTable.Columns.Count; ColumnIndex++)
{
sb.Append(iDataTable.Rows[RowIndex][ColumnIndex].ToString() + " \t");
}
sb.Append(Environment.NewLine);
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
return true;
}
catch
{
return false;
}
}
private bool DataSetToExcel1(DataSet iExportDataSet, string iExportFilePath)
{
try
{
DataTable iDataTable = iExportDataSet.Tables[0];
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
int rowindex = 1;
int collndex = 0;
excel.Application.Workbooks.Add(true);
foreach(DataColumn col in iDataTable.Columns)
{
collndex++;
excel.Cells[1, collndex] = col.ColumnName;
}
foreach(DataRow row in iDataTable.Rows)
{
rowindex++; collndex = 0;
foreach(DataColumn col in iDataTable.Columns)
{
collndex++;
excel.Cells[rowindex, collndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
excel.DisplayAlerts = false;
excel.ActiveWorkbook.SaveAs(iExportFilePath);
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
return true;
}
catch
{
return false;
}
}
private DataSet ListToDataSet(ObservableCollection< CClientInfo> iExportCorrectList)
{
DataSet iDataSet = new DataSet();
DataTable iDataTable = new DataTable();
iDataTable.Columns.Add(" 客户名称" , typeof(string));
iDataTable.Columns.Add(" 性别" , typeof(string));
iDataTable.Columns.Add(" 联系电话" , typeof(string));
iDataTable.Columns.Add(" 邮箱" , typeof(string));
int iRowCount = iClientInfoList.Count;
for (int iCount = 0; iCount < iRowCount; iCount++)
{
DataRow iDataRow = iDataTable.NewRow();
iDataRow[0] = iClientInfoList[iCount].iName;
iDataRow[1] = iClientInfoList[iCount].iSex;
iDataRow[2] = iClientInfoList[iCount].iTel;
iDataRow[3] = iClientInfoList[iCount].iEmail;
iDataRow[4] = iClientInfoList[iCount].iQQ;
iDataRow[5] = iClientInfoList[iCount].iCompany;
iDataRow[6] = iClientInfoList[iCount].iAddress;
iDataTable.Rows.Add(iDataRow);
}
iDataSet.Tables.Add(iDataTable);
return iDataSet;
}
private bool DataSetToExcel(DataSet iExportDataSet, string iExportFilePath)
{
try
{
DataTable iDataTable = iExportDataSet.Tables[0];
StreamWriter sw = new StreamWriter(iExportFilePath, false, Encoding.GetEncoding(" gb2312"));
StringBuilder sb = new StringBuilder();
for (int ColumnIndex = 0; ColumnIndex < iDataTable.Columns.Count; ColumnIndex++)
{
sb.Append(iDataTable.Columns[ColumnIndex].ColumnName.ToString() + " \t");
}
sb.Append(Environment.NewLine);
for (int RowIndex = 0; RowIndex < iDataTable.Rows.Count; RowIndex++)
{
for (int ColumnIndex = 0; ColumnIndex < iDataTable.Columns.Count; ColumnIndex++)
{
sb.Append(iDataTable.Rows[RowIndex][ColumnIndex].ToString() + " \t");
}
sb.Append(Environment.NewLine);
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
return true;
}
catch
{
return false;
}
}
private bool DataSetToExcel1(DataSet iExportDataSet, string iExportFilePath)
{
try
{
DataTable iDataTable = iExportDataSet.Tables[0];
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
int rowindex = 1;
int collndex = 0;
excel.Application.Workbooks.Add(true);
foreach(DataColumn col in iDataTable.Columns)
{
collndex++;
excel.Cells[1, collndex] = col.ColumnName;
}
foreach(DataRow row in iDataTable.Rows)
{
rowindex++; collndex = 0;
foreach(DataColumn col in iDataTable.Columns)
{
collndex++;
excel.Cells[rowindex, collndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
excel.DisplayAlerts = false;
excel.ActiveWorkbook.SaveAs(iExportFilePath);
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
return true;
}
catch
{
return false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询