c#中如何把数据库取出的数据写入指定的Excel文件中,在线等待,请高手帮忙!!!
c#中如何把数据库取出的数据写入指定的Excel文件中。请给代码看看,非常感谢! --------------------编程问答-------------------- DataSet ds = new Dataset();...
da.file(ds);
ds.WriteXml("data.xml");
Process.Start("excele.exe data.xml"); --------------------编程问答-------------------- public void cExportExcel(DataView dv)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls" ;
saveFileDialog1.FilterIndex = 0 ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CreatePrompt=true;
saveFileDialog1.Title="导出Excel文件到";
DateTime now=DateTime.Now;
saveFileDialog1.FileName=now.Year.ToString().PadLeft(2)
+now.Month.ToString().PadLeft(2,'0')
+now.Day.ToString().PadLeft(2,'0')+"_"
+now.Hour.ToString().PadLeft(2,'0')
+now.Minute.ToString().PadLeft(2,'0')
+now.Second.ToString().PadLeft(2,'0');
saveFileDialog1.ShowDialog();
Stream myStream;
myStream=saveFileDialog1.OpenFile();
//StreamWriter sw=new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream,System.Text.Encoding.GetEncoding("big5"));
String str="";
//写标题
for(int i=0;i<dv.Table.Columns.Count;i++)
{
if(i>0)
{
str+="\t";
}
str+=dv.Table.Columns[i].ColumnName;
}
sw.WriteLine(str);
//写内容
for(int rowNo=0;rowNo<dv.Count;rowNo++)
{
String tempstr="";
for(int columnNo=0;columnNo<dv.Table.Columns.Count;columnNo++)
{
if(columnNo>0)
{
tempstr+="\t";
}
//tempstr+=dg.Rows[rowNo,columnNo].ToString();
tempstr+=dv.Table.Rows[rowNo][columnNo].ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
} --------------------编程问答-------------------- /**//// <summary>
/// DataGridView导出到Excel
/// 操作步骤:
/// 1)先添加对Excel的引用。选择项目-〉添加引用-〉COM-〉添加Microsoft Excel 9.0
/// (不同的office讲会有不同版本的dll文件).
/// 2)引入using Microsoft.Office.Interop.Excel;和using System.Reflection;
///
/// </summary>
/// <param name="dgv"></param>
public void DBtoExcel(DataGridView dgv)
{
int rowCount = dgv.RowCount;
int columnCount = dgv.ColumnCount;
//
Microsoft.Office.Interop.Excel.Application exc = new Microsoft.Office.Interop.Excel.Application();
if (exc == null)
{
throw new Exception("Excel无法启动");
}
//
exc.Visible = true;
//
Workbooks workbooks = exc.Workbooks;
//
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
//
Sheets sheets = exc.Sheets;
_Worksheet worksheet = (_Worksheet)sheets[1];
if (worksheet == null)
{
throw new Exception("Worksheet error");
}
//
Range r = worksheet.get_Range("A1", Missing.Value);
if (r == null)
{
MessageBox.Show("Range无法启动");
throw new Exception("Range error");
}
//以上是一些例行的初始化工作,下面进行具体的信息填充
//填充标题
int ColIndex = 1;
foreach (DataGridViewColumn dHeader in dgv.Columns)
{
worksheet.Cells[1, ColIndex++] = dHeader.HeaderText;
}
//获取DataGridView中的所有行和列的数值,填充到一个二维数组中.
object[,] myData = new object[rowCount + 1, columnCount];
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
myData[i, j] = dgv[j,i].Value; //这里的获取注意行列次序
}
}
//将填充好的二维数组填充到Excel对象中.
r = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[rowCount + 1, columnCount]);
r.Value2 = myData;
} --------------------编程问答-------------------- 一楼的方法更是别出心裁
但不知道效率怎么样 --------------------编程问答-------------------- 多谢个位高手相助,我现在使用的方法是:
Excel.Application excel = new Excel.Application ();//引用Excel对象
excel.Application.Workbooks.Add ( true );//引用Excel工作簿
excel.Visible = true ;//使Excel可视
在项目中已经打开Add Reference对话框,选择COM栏,之后在COM列表中找到"Microsoft Excel 9.0 Object Library"(Office 2000),但无法打开EXCLE文件。 --------------------编程问答-------------------- 为什么我的COM列表中没有Microsoft Excel 9.0 Object Library或类似的excel的东西?
--------------------编程问答-------------------- 先引用Excel的COM 就可以了
public void ExcelDaoChu()
{
try
{
string filename;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择将导出的EXCEL文件存放路径";
sfd.Filter="excel文档(*.xls)|*.xls";
sfd.ShowDialog();
if (sfd.FileName != "")
{
if(sfd.FileName.LastIndexOf(".xls")<=0)
{
sfd.FileName=sfd.FileName+".xls";
}
filename=sfd.FileName;
if(System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机器未安装Excel");
return;
}
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
DataTable dt1 = (DataTable)dataGrid1.DataSource;
float percent=0;
long rowRead=0;
if(dt1==null)
{
MessageBox.Show("DataGrid没有数据哦");
return;
}
long totalCount=dt1.Rows.Count;
this.progressBar1.Visible=true;
for (int i = 0; i < dt1.Rows.Count; i++)
{
this.progressBar1.Value=i;
for (int j = 0; j < dt1.Columns.Count; j++)
{
if (i == 0)
{
worksheet.Cells[1, j + 1] = dt1.Columns[j].ColumnName;
}
worksheet.Cells[i + 2, j + 1] = dt1.Rows[i][j].ToString();
}
rowRead++;
percent=((float)(100*rowRead))/totalCount;
this.progressBar1.Text= "正在导出数据["+ percent.ToString("0.00") +"%]...";
}
this.progressBar1.Visible=false;
workbook.Saved = true;
workbook.SaveCopyAs(filename);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
MessageBox.Show("导出Excel完成!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//button2_Click(null,null);
}
}
catch(Exception ex)
{
MessageBox.Show("导出Excel失败!"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
} --------------------编程问答-------------------- 学习了
补充:.NET技术 , C#