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

winform导出excel的更多相关文章

  1. Winform 导出Excel

    private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e) { ) { var saveFileDialog1 = ...

  2. c# winform导出Excel

    //需要注意添加引用Microsoft.Office.Interop.Excel.dll string fileName =DateTime.Now.Year+ DateTime.Now.Month+ ...

  3. C#导入导出Excel表的数据

    一:C#导入导出EXCEL文件的类 代码如下: 首先将Microsoft Excel 14.0 Object Library 引用导入 using System; using System.Data; ...

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

    思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...

  5. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

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

    还真没做过winform的导出导入,今天上网百度了一下.结果--- 所以还是我自己写个吧.之前做过web的,半搬半做就OK. 1添加引用:Aspose.Cells.dll(我们就叫工具包吧,可以从网上 ...

  7. 懒人小工具1:winform自动生成Model,Insert,Select,Delete以及导出Excel的方法

       懒人小工具2:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法    github地址:https://github.com/Jimmey-Jiang/J ...

  8. NPOI导入导出EXCEL通用类,可直接使用在WinForm项目中

    由于XSSFWorkbook类型的Write方法限制,Write完成后就自动关闭流数据,所以无法很好的支持的Web模式,网上目前也未找到好的解决方案. 注意:若直接使用在WinForm项目中,必需先下 ...

  9. C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)

    在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便.具体可以参考其他 http://www.aspose.com/docs/display/cells ...

随机推荐

  1. multiselect多选下拉框

    具体实现 <input type="hidden" id="q_dueDay" name="q_dueDay" value=" ...

  2. git submodule添加、更新和删除

    添加 git submodule add <url> <path> url:替换为自己要引入的子模块仓库地址 path:要存放的本地路径 执行添加命令成功后,可以在当前路径中看 ...

  3. JVM之JVM体系结构

    JVM是运行在操作系统之上的,它与硬件没有直接的交互 下图运行时数据区灰色代表线程私有,亮色(方法区和堆)代表所有线程共享. 1.类装载器ClassLoader 负责加载class文件,class文件 ...

  4. 2019icpc徐州站 Cat 计蒜客 - 42540 && The Answer to the Ultimate Question of Life, The Universe, and Everything. 计蒜客 - 42545

    VJ链接:https://vjudge.net/contest/412095#problem/A Cat 计蒜客 - 42540 题意: 给你一个区间[L,R],给你现在拥有的钱S.你需要从[L,R] ...

  5. HDU4366 Successor【dfs序 分块】

    HDU4366 Successor 题意: 给出一棵根为\(1\)的树,每个点有两个权值\(x,y\),每次询问一个点的子树中\(x\)比这个点的\(x\)大且\(y\)值最大的那个点 题解: 如果以 ...

  6. 【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)

    题意:给出 A%9973 和 B,求(A/B)%9973的值. 解法:拓展欧几里德求逆元.由于同余的性质只有在 * 和 + 的情况下一直成立,我们要把 /B 转化为 *B-1,也就是求逆元. 对于 B ...

  7. Pokémon Army (easy version) CodeForces - 1420C1 dp

    题意: 给你一个长度为n个序列v,你需要从中找一个子序列.这个子序列的值等于:子序列中奇数下标的值-偶数下标的值 你需要使得这个值尽可能大,让你输出这个最大值 题解: dp[i][0]表示:在原序列从 ...

  8. Codeforces Round #651 (Div. 2) A Maximum GCD、B GCD Compression、C Number Game、D Odd-Even Subsequence

    A. Maximum GCD 题意: t组输入,然后输入一个n,让你在区间[1,n]之间找出来两个不相等的数a,b.求出来gcd(a,b)(也就是a,b最大公约数).让你求出来最大的gcd(a,b)是 ...

  9. hdu4217 Data Structure?

    Problem Description Data structure is one of the basic skills for Computer Science students, which i ...

  10. ArcGIS处理栅格数据(一)

    一.建立影像金字塔 ArcToolbox--数据管理工具--栅格--栅格属性--构建金字塔(pyramid) 说明:该方式一次只能为一张影像数据建立影像金字塔. ArcToolbox--数据管理工具- ...