永发信息网

请问如何把dataset里面的表导出到excel中?

答案:3  悬赏:10  手机版
解决时间 2021-03-20 14:53
  • 提问者网友:回忆在搜索
  • 2021-03-19 21:24
请问如何把dataset里面的表导出到excel中?
最佳答案
  • 五星知识达人网友:酒者煙囻
  • 2021-03-19 22:57
将DataSet中的DataTable作为参数传入下面的方法,再将你要保存的完整路径名作为第二个参数传入下面的方法,就可以将数据导入到Excel中
public void CreateExcel(DataTable dt,string fileName)
{
System.Diagnostics.Process[] arrProcesses;
arrProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process myProcess in arrProcesses)
{
myProcess.Kill();
}

Object missing = Missing.Value;
Microsoft.Office.Interop.Excel.Application m_objExcel =

new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks m_objWorkBooks = m_objExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook m_objWorkBook = m_objWorkBooks.Add(true);
Microsoft.Office.Interop.Excel.Sheets m_objWorkSheets = m_objWorkBook.Sheets; ;
Microsoft.Office.Interop.Excel.Worksheet m_objWorkSheet =

(Microsoft.Office.Interop.Excel.Worksheet)m_objWorkSheets[1];
int intFeildCount = dt.Columns.Count;
for (int col = 0; col < intFeildCount; col++)
{
m_objWorkSheet.Cells[1, col + 1] = dt.Columns[col].ToString();
}
for (int intRowCount = 0; intRowCount < dt.Rows.Count; intRowCount++)
{
for (int intCol = 0; intCol < dt.Columns.Count; intCol++)
{
m_objWorkSheet.Cells[intRowCount + 2, intCol + 1] = "'"+dt.Rows[intRowCount][intCol].ToString();
}
}

if (File.Exists(fileName))
{
File.Delete(fileName);
}
m_objWorkBook.SaveAs(fileName, missing, missing, missing, missing,

missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);

m_objExcel = null;

}
全部回答
  • 1楼网友:冷風如刀
  • 2021-03-20 00:00
以前用到了,再网上找的

///
/// 把DataGridView到导出到Excel中
///

/// 数据源DataGridView
/// 保存文件名称
/// 是否显示Excel界面
/// 返回异常信息
/// 导出是否成功
private bool ExportForDataGridview(System.Windows.Forms.DataGridView gridView, string fileName, bool isShowExcle, out string exceptionInfo)
{
bool isok = false;
exceptionInfo = "";
Microsoft.Office.Interop.Excel.Application app = null;
try
{
//建立Excel对象
app = new Microsoft.Office.Interop.Excel.Application();
}
catch
{
exceptionInfo = "无法创建Excel对象,请确保您的机器安装有MicroSoft Office 2003。";
isok = false;
}
if (app != null)
{
try
{
app.Visible = isShowExcle;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
exceptionInfo = "无法创建Excel工作表,未知错误。";
isok = false;
}
else
{
string sLen = "";
//取得最后一列列名
char H = (char)(64 + gridView.ColumnCount / 26);
char L = (char)(64 + gridView.ColumnCount % 26);
if (gridView.ColumnCount < 26)
{
sLen = L.ToString();
}
else
{
sLen = H.ToString() + L.ToString();
}

//标题
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[gridView.ColumnCount];
for (int i = 0; i < gridView.ColumnCount; i++)
{
asCaption[i] = gridView.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption;

//数据
object[] obj = new object[gridView.Columns.Count];
for (int r = 0; r < gridView.RowCount - 1; r++)
{
for (int l = 0; l < gridView.Columns.Count; l++)
{
if (gridView[l, r].ValueType == typeof(DateTime))
{
obj[l] = gridView[l, r].Value.ToString();
}
else if (gridView[l, r].ValueType == typeof(String))
{
obj[l] = "'" + gridView[l, r].Value.ToString();
}
else
{
obj[l] = gridView[l, r].Value;
}
}
string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;
}

//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true;
isok = true;
}
}
catch (Exception err)
{
exceptionInfo = err.Message;
isok = false;
}
finally
{
if (app != null)
{
//关闭
app.UserControl = false;
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
}
}
}
return isok;

}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯