110,545
社区成员
发帖
与我相关
我的任务
分享
private void ReportOut(string filename,string strsql)
{
//第一步:导出数据表
oracomm.CommandText = strsql;
orareader = oracomm.ExecuteReader();
//第二步:创建excel文件
GC.Collect();
Microsoft.Office.Interop.Excel.Application excel;
Workbook xBk;
Worksheet xSt = null;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
//第三步:填充excel文件
//定义循环中要使用的变量
int sheetIndex = 1;
int rowIndex = 1;
int colIndex = 1;
while (orareader.Read())
{
//首行时,添加新的工作表,添加标题栏
if (rowIndex == 1)
{
//创建一个Sheet
if (null == xSt)
{
//第一个工作表创建位置
xSt = (Worksheet)xBk.Worksheets[1];
}
else
{
//新的工作表的创建位置
xSt = (Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, 1, Type.Missing);
}
//设置工作表名
xSt.Name = "数据明细" + sheetIndex.ToString();
//填充标题栏
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, orareader.FieldCount + 1]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题居中对齐
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, orareader.FieldCount + 1]).Font.Bold = true;//设置标题为粗体
for (colIndex = 1; colIndex <= orareader.FieldCount; colIndex++)
{
xSt.Cells[1, colIndex + 1] = orareader.GetName(colIndex - 1);
}
rowIndex++;
}
//填充数据
xSt.Cells[rowIndex, 1] = rowIndex - 1;
for (colIndex = 1; colIndex <= orareader.FieldCount; colIndex++)
{
if (orareader.GetFieldType(colIndex - 1) == System.Type.GetType("System.String"))
{
xSt.Cells[rowIndex, colIndex + 1] = "'" + orareader[colIndex - 1];
}
else
{
xSt.Cells[rowIndex, colIndex + 1] = orareader[colIndex - 1];
}
}
if ((rowIndex - 1) % 100 == 0)
{
txtInfo.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "已导入数据" + (rowIndex - 1).ToString() + "条!\r\n");
xBk.Save();
}
//如果超过限定行数,添加新表
if (rowIndex >= 60000)
{
//使用最佳宽度
Range allDataWithTitleRange = xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex, colIndex + 1]);
allDataWithTitleRange.Select();
allDataWithTitleRange.Columns.AutoFit();
allDataWithTitleRange.Borders.LineStyle = 1;//将导出Excel加上边框
sheetIndex++;
rowIndex = 1;
}
else
{
rowIndex++;
}
}
//第四步:保存excel文件
xBk.SaveCopyAs("D:\\" + filename + ".xls");
xBk.Close(false, null, null);
excel.Quit();
orareader.Dispose();
oracomm.Dispose();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
}
string fileName,filePath,strLine;
FileStream objFileStream;
StreamWriter objStreamWriter;
fileName = "NewFile.xls";
filePath = "c:\\"+fileName;
objFileStream = new FileStream(filePath,FileMode.Create,FileAccess.ReadWrite);
objStreamWriter = new StreamWriter(objFileStream,System.Text.Encoding.Default);
objStreamWriter.AutoFlush=true;
strLine = "第一列\t第二列\t第三列";
objStreamWriter.WriteLine(strLine);
strLine = "";
DataSet ds = new DataSet();
ds=....(查询出来的数据)
for(int j=0;j<=ds.Tables[0].Rows.Count-1;j++)
{
strLine += "\r";
for (int i = 0; i < ds.Tables[0].Columns.Count-1; i++)
{
strLine = strLine + ds.Tables[0].Rows[j][i].ToString()+"\t";
}
strLine += ds.Tables[0].Rows[j][ds.Tables[0].Columns.Count-1].ToString();
strLine = strLine.Trim();
objStreamWriter.WriteLine(strLine);
strLine="";
}
objStreamWriter.Flush();
objFileStream.Flush();
objStreamWriter.Close();
objFileStream.Close();
//填充数据区
for (j = 1; j <= dtOut.Columns.Count; j++)
{
if (dtOut.Columns[j - 1].DataType == System.Type.GetType("System.String"))
{
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, j] = "'" + dtOut.Rows[i - 1][j - 1].ToString();
}
}
else if (dtOut.Columns[j - 1].DataType == System.Type.GetType("System.DateTime"))
{
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, j] = "'" + ((DateTime)dtOut.Rows[i - 1][j - 1]).ToString("yyyy-MM-dd HH:mm:ss");
}
}
else //这里忘记加上一个else了
{
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, j] = dtOut.Rows[i - 1][j - 1];
}
}
}
private void ReportOutC(string filename, string strsql)
{
//第一步:导出数据表
oracomm.CommandText = strsql;
orareader = oracomm.ExecuteReader();
System.Data.DataTable dtOut = new System.Data.DataTable();
dtOut.Load(orareader);
orareader.Dispose();
txtInfo.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "数据已经读取到DataTable中!\r\n");
txtInfo.ScrollToCaret();
object[,] ObjData = new object[dtOut.Rows.Count + 1, dtOut.Columns.Count + 1];
int i,j;
ObjData[0, 0] = "";
//填充标题行
for (j = 1; j <= dtOut.Columns.Count; j++)
{
ObjData[0, j] = dtOut.Columns[j - 1].ColumnName;
}
//填充行号列
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, 0] = i;
}
//填充数据区
for (j = 1; j <= dtOut.Columns.Count; j++)
{
if (dtOut.Columns[j - 1].DataType == System.Type.GetType("System.String"))
{
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, j] = "'" + dtOut.Rows[i - 1][j - 1].ToString();
}
}
else if (dtOut.Columns[j - 1].DataType == System.Type.GetType("System.DateTime"))
{
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, j] = "'" + ((DateTime)dtOut.Rows[i - 1][j - 1]).ToString("yyyy-MM-dd HH:mm:ss");
}
}
{
for (i = 1; i <= dtOut.Rows.Count; i++)
{
ObjData[i, j] = dtOut.Rows[i - 1][j - 1];
}
}
}
txtInfo.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + "数据已经填充到数组中!\r\n");
txtInfo.ScrollToCaret();
//第二步:创建excel文件
GC.Collect();
Microsoft.Office.Interop.Excel.Application excel;
Workbook xBk;
Worksheet xSt = null;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
//第三步:填充excel文件
//定义循环中要使用的变量
//填充标题栏
xSt = (Worksheet)xBk.Worksheets[1];
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, dtOut.Columns.Count + 1]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题居中对齐
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[1, dtOut.Columns.Count + 1]).Font.Bold = true;//设置标题为粗体
xSt.get_Range(xSt.Cells[1, 1], xSt.Cells[dtOut.Rows.Count + 1, dtOut.Columns.Count + 1]).Value2 = ObjData;
//第四步:保存excel文件
xBk.SaveCopyAs("D:\\" + filename + ".xls");
xBk.Close(false, null, null);
excel.Quit();
orareader.Dispose();
oracomm.Dispose();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
}