用c#怎么将access数据库中某一个表的数据导出成execl文件保存,用C#写,代码是什么?望高手指点 15
2个回答
展开全部
#region 导出
if (dataGridView4.Rows.Count > 0)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "xls";
saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.FileName = DateTime.Now.Date.ToString("yyyy-MM-dd") +"出入记录";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出到EXCEL";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == "")
return;
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
for (int i = 1; i < dataGridView4.ColumnCount; i++)
{
if (i > 1)
{
str += "\t";
}
str += dataGridView4.Columns[i].HeaderText;
}
sw.WriteLine(str);
for (int j = 0; j < dataGridView4.Rows.Count; j++)
{
string tempStr = "";
for (int k = 1; k < dataGridView4.Columns.Count; k++)
{//数据源是将datagridview中的数据换成你access中表中的数据,你直接
//查询到一datatable表,取值就可以了
if (k > 1)
{
tempStr += "\t";
}
tempStr += dataGridView4.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
else
{
MessageBox.Show("无记录!", "提示");
return;
}
#endregion
if (dataGridView4.Rows.Count > 0)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "xls";
saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.FileName = DateTime.Now.Date.ToString("yyyy-MM-dd") +"出入记录";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出到EXCEL";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == "")
return;
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
for (int i = 1; i < dataGridView4.ColumnCount; i++)
{
if (i > 1)
{
str += "\t";
}
str += dataGridView4.Columns[i].HeaderText;
}
sw.WriteLine(str);
for (int j = 0; j < dataGridView4.Rows.Count; j++)
{
string tempStr = "";
for (int k = 1; k < dataGridView4.Columns.Count; k++)
{//数据源是将datagridview中的数据换成你access中表中的数据,你直接
//查询到一datatable表,取值就可以了
if (k > 1)
{
tempStr += "\t";
}
tempStr += dataGridView4.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
else
{
MessageBox.Show("无记录!", "提示");
return;
}
#endregion
展开全部
首先要把数据从ACCESS中提取出来放在一个DataGridView 里,然后从DataGridView 里到处到execl的代码如下:
public static void GetExecl(DataGridView dgv)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("无数据,不能导出excel", "导出");
return;
}
//建立Excel对象
try
{
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + dgv[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
}
excel.SaveWorkspace("d:\\shee1.xls");\\这里写导出到哪里
excel.Quit();
MessageBox.Show("导出完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
// excel.ThisWorkbook.Close(true,"d:\\shee1.xls",excel.Sheets[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "导出excel出错", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
}
public static void GetExecl(DataGridView dgv)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("无数据,不能导出excel", "导出");
return;
}
//建立Excel对象
try
{
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + dgv[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
}
excel.SaveWorkspace("d:\\shee1.xls");\\这里写导出到哪里
excel.Quit();
MessageBox.Show("导出完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
// excel.ThisWorkbook.Close(true,"d:\\shee1.xls",excel.Sheets[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "导出excel出错", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询