winform导出excel

public void AllDataSetToExcel(DataSet ds)
{
string saveFileName = "";
bool fileSaved = false;
Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "库存 " + DateTime.Today.ToString("yyyy-MM-dd");
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法启动Excel,可能您的机子未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;

// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = ds.Tables[0].Columns.Count;
int RowCount = ds.Tables[0].Rows.Count;

// *****************获取数据*********************
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount];
// 获取列标题
for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
{
if (ds.Tables[0].Columns[k].ColumnName == "sgAcName") ds.Tables[0].Columns[k].ColumnName = "操作员";
if (ds.Tables[0].Columns[k].ColumnName == "sgCode") ds.Tables[0].Columns[k].ColumnName = "美国转运单号";
if (ds.Tables[0].Columns[k].ColumnName == "sgLbs") ds.Tables[0].Columns[k].ColumnName = "重量(公斤)";
if (ds.Tables[0].Columns[k].ColumnName == "sgPin") ds.Tables[0].Columns[k].ColumnName = "用户标识(字母)";
if (ds.Tables[0].Columns[k].ColumnName == "sgStoreCode") ds.Tables[0].Columns[k].ColumnName = "仓储位置(字母+数字)";
if (ds.Tables[0].Columns[k].ColumnName == "sgPort") ds.Tables[0].Columns[k].ColumnName = "仓库";
if (ds.Tables[0].Columns[k].ColumnName == "sgTPRemark") ds.Tables[0].Columns[k].ColumnName = "内部备注";
if (ds.Tables[0].Columns[k].ColumnName == "sgTime") ds.Tables[0].Columns[k].ColumnName = "操作日期";
if (ds.Tables[0].Columns[k].ColumnName == "sgZbDate") ds.Tables[0].Columns[k].ColumnName = "收件日期";
objData[RowIndex, colIndex++] = ds.Tables[0].Columns[k].ColumnName.ToString();
}

// 获取具体数据
for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{

objData[RowIndex + 1, colIndex] = ds.Tables[0].Rows[RowIndex][colIndex];
}

}
//********************* 写入Excel*******************
range = worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)xlApp.Cells[1, 1], (Microsoft.Office.Interop.Excel.Range)xlApp.Cells[RowCount + 1, colCount]);
range.Value2 = objData;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
Application.DoEvents();

// //*******************设置输出格式******************************

////设置顶部説明
//range = worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)xlApp.Cells[1, 1], (Microsoft.Office.Interop.Excel.Range)xlApp.Cells[1, colCount]);
//range.MergeCells = true;
//range.RowHeight = 38;
//range.Font.Bold = true;
//range.Font.Size = 14;
//range.Font.ColorIndex = 10;//字体颜色
//xlApp.ActiveCell.FormulaR1C1 = "导入记录查询结果";

//特殊数字格式
range = worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)xlApp.Cells[2, 8], (Microsoft.Office.Interop.Excel.Range)xlApp.Cells[RowCount + 1, 8]);
range.NumberFormat = "yyyy-MM-dd hh:mm:ss";

//特殊数字格式
range = worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)xlApp.Cells[2, 9], (Microsoft.Office.Interop.Excel.Range)xlApp.Cells[RowCount + 1, 9]);
range.NumberFormat = "yyyy-MM-dd hh:mm:ss";

xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
range = worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)xlApp.Cells[1, 1], (Microsoft.Office.Interop.Excel.Range)xlApp.Cells[1, colCount]);
// range.Interior.ColorIndex = 10;//背景色
range.Interior.Color = Color.Yellow;
range.Font.Bold = true;
range.RowHeight = 20;
//range.Cells.Columns.AutoFit();

((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 1]).ColumnWidth = 15;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 2]).ColumnWidth = 35;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 3]).ColumnWidth = 15;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 4]).ColumnWidth = 15;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 5]).ColumnWidth = 12;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 6]).ColumnWidth = 10;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 7]).ColumnWidth = 28;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 8]).ColumnWidth = 28;
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 9]).ColumnWidth = 28;

//***************************保存 * *********************
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveAs(saveFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8);
fileSaved = true;
MessageBox.Show("Export Excel success");
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!/n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁

}
#endregion

时间: 2024-10-30 04:07:44

winform导出excel的相关文章

NPOI winform导出Excel

引用命名空间 using System.IO;using NPOI.XSSF.UserModel;using NPOI.HSSF.Util; /// <summary> /// 导出Excel的操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenE

c# winform导出Excel

//需要注意添加引用Microsoft.Office.Interop.Excel.dll string fileName =DateTime.Now.Year+ DateTime.Now.Month+ DateTime.Now.Day + ".xls"; if (showView.Rows.Count > 0) { string saveFileName = ""; //bool fileSaved = false; SaveFileDialog saveDi

winform导入导出excel,后台动态添加控件

思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(string extension = Path.GetExtension(fileDialog.FileName).ToLower();),并设置允许后缀文件名: 3,NPOI转datetable,遍历tatetable转成实体类列表并入库: 导出: 1, 创建提示用户保存类,SaveFileDial

导出Excel方法(winform或web)

一.winform形式导出Excel 此方法适用于winform项目导出Excel,使用前需要引用Excel.dll,此处是直接用ds导出Excel,导出方法类GetExport如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.IO; 5 using System.Text; 6 using System.Windows.Forms; 7 using Excel;

C# winform 导出导入Excel/Doc 完整实例教程[网上看到的]

还真没做过winform的导出导入,今天上网百度了一下.结果--- 所以还是我自己写个吧.之前做过web的,半搬半做就OK. 1添加引用:Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 Aspose C# 导出Excel 实例”一文中的说.这里你暂时也可不理会它.) Aspose.Cells.dll  和中文说明 下载地址: http://item.taobao.com/auction/item_detail-

winform导出dataviewgrid数据为excel的方法

本文实例讲述了winform导出dataviewgrid数据为excel的方法.分享给大家供大家参考.具体实现方法如下: 代码如下: #region 导出dataViewGrid视图中的数据为xls格式 private void btnExportList_Click(object sender, EventArgs e) { string fname = string.Empty; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter =

winform 导出datagridview 到excel

数据不多可以用下面的方式方法,如果数据较大,不建议这样使用,可能会比较卡如果电脑上没有Microsoft.Office.Interop.Excel.dll去找DLL下载站下载即可 需要先导入这个dll的引用 呈上代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; usi

C# WinForm使用XML+XSLT导出Excel数据表格

最终导出EXCEL样式如下图: 1.将最终样式的Excel另存为XML电子表格2003,如下图: 2.使用记事本打开保存后的XML电子表格2003格式的文件,得到文件后台源码,如下: 1 <?xml version="1.0"?> 2 <?mso-application progid="Excel.Sheet"?> 3 <Workbook xmlns="urn:schemas-microsoft-com:office:spre

C#winform导出数据到Excel的类

1 /// <summary> 2 /// 构造函数 3 /// </summary> 4 public ExportData() 5 { 6 7 } 8 /// <summary> 9 /// 保存文件名 10 /// </summary> 11 private string savefilename = ""; 12 /// <summary> 13 /// 准备导出的数据容器 14 /// </summary>