标签:
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
标签:
原文地址:http://www.cnblogs.com/panjuan/p/4757592.html