带格式的数据导出
public void OpenExcelFile()
{
try
{
Excel.Application excelapp;
excelapp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook book = excelapp.Workbooks.Open(Application.StartupPath + @"\ReportFile\018.xlt",
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet st1 = (Excel.Worksheet)book.Worksheets[1]; int rowBase = 3;
int colBase = 1;
int rowIndex = rowBase;
int colIndex = colBase; foreach (DataRowView row in this.m_DataView)
{
Excel.Range range = (Excel.Range)st1.Rows[rowIndex + 1, Missing.Value];
range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Missing.Value); st1.Cells[rowIndex, colIndex] ="";
colIndex++; st1.Cells[rowIndex, colIndex] = "'" + row["YeWuLeiXings"].ToString();
colIndex++; st1.Cells[rowIndex, colIndex] = "'" + row["QuYuHuaFen"].ToString();
colIndex++; st1.Cells[rowIndex, colIndex] = "'" + row["BeiZhu"].ToString(); rowIndex++;
colIndex = colBase;
} excelapp.Visible = true;
}
catch (Exception ex)
{
Wip.Common.ShowExecResult(Wip.Common.GetExecResult(ex), false);
}
finally
{
Cursor.Current = Cursors.Default;
} }
public void OpenExcelFile()
{
try
{
Excel.Application excelapp;
excelapp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook book = excelapp.Workbooks.Open(Application.StartupPath + @"\ReportFile\018.xlt",
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet st1 = (Excel.Worksheet)book.Worksheets[1]; int rowBase = 3;
int colBase = 1;
int rowIndex = rowBase;
int colIndex = colBase; foreach (DataRowView row in this.m_DataView)
{
Excel.Range range = (Excel.Range)st1.Rows[rowIndex + 1, Missing.Value];
range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Missing.Value); st1.Cells[rowIndex, colIndex] ="";
colIndex++; st1.Cells[rowIndex, colIndex] = "'" + row["YeWuLeiXings"].ToString();
colIndex++; st1.Cells[rowIndex, colIndex] = "'" + row["QuYuHuaFen"].ToString();
colIndex++; st1.Cells[rowIndex, colIndex] = "'" + row["BeiZhu"].ToString(); rowIndex++;
colIndex = colBase;
} excelapp.Visible = true;
}
catch (Exception ex)
{
Wip.Common.ShowExecResult(Wip.Common.GetExecResult(ex), false);
}
finally
{
Cursor.Current = Cursors.Default;
} }
解决方案 »
- C/S 架构下的Server怎么实现
- 散分~~~~~~~
- C#菜鸟问题 求助
- asp.net当用户点击浏览页面的时候获取客户端IP,存到数据库中
- datagridview有没有gridview中的RowBound事件
- 在 private void frm_wzbf_Load(object sender, EventArgs e)下同时实现分别查询两个不同的表,将其查询结果一个数据读进textbox1,另一个数据读进
- 客户端提交数据到服务器的问题
- 如何使得Hashtable对象的value是一个集合呢?
- C#连接oracle9时出现了这个问题(报了异常),大家帮帮忙啊
- xml致命一击,我已经不堪憔悴,望神助!xml中使用html内容!
- C# System.IO.Directory.GetFile问题
- C#里可以申明一个引用变量吗?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ExcelExports
...{
public class ExcelExports
...{
/**//// <summary>
/// 把DataGridView到处到Excel中
/// </summary>
/// <param name="gridView">目标DataGridView</param>
/// <param name="fileName">保存文件名称</param>
/// <param name="isShowExcle">是否显示Excel界面</param>
/// <returns>导出是否成功</returns>
public static bool ExportForDataGridview(DataGridView gridView,string fileName,bool isShowExcle)
...{
//建立Excel对象
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
try
...{
if (app == null)
...{
return false;
}
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)
...{
return false;
}
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
...{
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;
}
finally
...{
//关闭
app.UserControl = false;
app.Quit();
}
return true; }
}
}
//声明一个
Excel.Application myExcel=new Excel.ApplicationClass();
myExcel.Visible=false; //创建一个导入数据的object,保证所有的线程都是安全的
object miss=Missing.Value;
Excel.Workbook wb=myExcel.Workbooks.Add(miss);
//建立一个sheet
Excel.Worksheet ws=(Excel.Worksheet)wb.Sheets.get_Item(1);
//获得当前sdatagrid里面的行和列
int RowCont=ds.Tables[0].Rows.Count;
int ColCont=ds.Tables[0].Columns.Count; //将信息写入数组里面
string[,] myarr=new string[RowCont,ColCont];
for(int i=0;i<RowCont;i++)
{
for(int j=0;j<ColCont;j++)
{
myarr[i,j]=ds.Tables[0].Rows[i][j].ToString();
}
} //将数组写入里面
Excel.Range range=(Excel.Range)ws.Cells[1,1];
range=(Excel.Range)range.get_Item(RowCont,ColCont);
range.Value2=myarr; //
wb.SaveAs("D:\\"+DateTime.Now.ToLongDateString()+".xls",miss,miss,miss,miss,miss,Excel.XlSaveAsAccessMode.xlExclusive,miss,miss,miss,miss,miss); if(wb.Saved)
{
wb.Close(null,null,null);
myExcel.Workbooks.Close();
myExcel.Quit();
} if(range==null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range=null;
}
if(ws==null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws=null;
}
if(wb==null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb=null;
}
if(myExcel==null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myExcel=null;
}
GC.Collect();