62,041
社区成员
发帖
与我相关
我的任务
分享
try
{
if (saveFileName.IndexOf(":") < 0) return false; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
object missing = System.Reflection.Missing.Value;
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
return false;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1
Microsoft.Office.Interop.Excel.Range range;
string strCaption = saveFileName.Remove(0, saveFileName.LastIndexOf('\\') + 1);
strCaption = strCaption.Remove(strCaption.Length - 4, 4);
long totalCount = myDataTable.Rows.Count;
long rowRead = 0;
float percent = 0;
range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, 1]];
//range = (Microsoft.Office.Interop.Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1],
// worksheet.Cells[2, myDataTable.Columns.Count]); //标题占用前两行
range.Merge(missing); //合并
// range.Font.Bold = false; //粗体设置
// range.Font.Size = 16; //字体大小设置
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //水平对齐设置
range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; //垂直对齐设置
//range.FormulaR1C1 = 公式; //公式设置
//range.ColumnWidth = 宽度; //列宽设置
//range.RowHeight = 行高; //行高
worksheet.Cells[1, 19] = strCaption;
#region 写入字段
worksheet.Cells[1, 1] = "钻石编号";
worksheet.Cells[1, 2] = "证书类型";
worksheet.Cells[1, 3] = "证书编号";
worksheet.Cells[1, 4] = "形状";
worksheet.Cells[1, 5] = "重量";
worksheet.Cells[1, 6] = "颜色";
worksheet.Cells[1, 7] = "切工";
worksheet.Cells[1, 8] = "净度";
worksheet.Cells[1, 9] = "对称";
worksheet.Cells[1, 10] = "抛光";
worksheet.Cells[1, 11] = "火彩";
worksheet.Cells[1, 12] = "尺寸";
worksheet.Cells[1, 13] = "台面百分比";
worksheet.Cells[1, 14] = "深度百分比";
worksheet.Cells[1, 15] = "腰厚百分比";
worksheet.Cells[1, 16] = "冠部百分比";
worksheet.Cells[1, 17] = "荧光";
worksheet.Cells[1, 18] = "价格";
worksheet.Cells[1, 19] = "地区";
//range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[4, i + 1];
// range.Interior.ColorIndex = 15;
//range.Font.Bold = true;
range.ColumnWidth = 15;
#endregion
//写入数值
for (int r = 0; r < myDataTable.Rows.Count; r++)
{
worksheet.Cells[r + 2, 1] = myDataTable.Rows[r]["DiamNumber"]; //钻石编号
worksheet.Cells[r + 2, 2] = myDataTable.Rows[r]["DiamCertificate"]; //证书类型
worksheet.Cells[r + 2, 3] = myDataTable.Rows[r]["DiamCertificateNo"]; //证书编号
worksheet.Cells[r + 2, 4] = myDataTable.Rows[r]["DiamShape"]; //形状
worksheet.Cells[r + 2, 5] = myDataTable.Rows[r]["DiamCt"]; //重量
worksheet.Cells[r + 2, 6] = myDataTable.Rows[r]["DiamColor"]; //颜色
worksheet.Cells[r + 2, 7] = myDataTable.Rows[r]["DiamCut"]; //切工
worksheet.Cells[r + 2, 8] = myDataTable.Rows[r]["DiamClarity"]; //净度
worksheet.Cells[r + 2, 9] = myDataTable.Rows[r]["DiamSymmetry"]; //对称
worksheet.Cells[r + 2, 10] = myDataTable.Rows[r]["DiamPolish"]; //抛光
worksheet.Cells[r + 2, 11] = myDataTable.Rows[r]["DiamLuster"]; //火彩
worksheet.Cells[r + 2, 12] = myDataTable.Rows[r]["DiamSize"]; //尺寸
worksheet.Cells[r + 2, 13] = myDataTable.Rows[r]["DiamTable"]; //台面百分比
worksheet.Cells[r + 2, 14] = myDataTable.Rows[r]["DiamDepth"]; //深度百分比
worksheet.Cells[r + 2, 15] = myDataTable.Rows[r]["DiamGirdle"]; //腰厚百分比
worksheet.Cells[r + 2, 16] = myDataTable.Rows[r]["DiamCrown"]; //冠部百分比
worksheet.Cells[r + 2, 17] = myDataTable.Rows[r]["Diamfluor"]; //荧光
worksheet.Cells[r + 2, 18] = myDataTable.Rows[r]["DiamPattiPrice"]; //价格
worksheet.Cells[r + 2, 19] = myDataTable.Rows[r]["DiaRelatShop"]; //地区
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
}
worksheet.SaveAs(saveFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
range = worksheet.Range[worksheet.Cells[4, 1], worksheet.Cells[myDataTable.Rows.Count + 1, myDataTable.Columns.Count]];
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
if (myDataTable.Columns.Count > 1)
{
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
}
workbook.Close(missing, missing, missing);
xlApp.Quit();
return true;
}
catch (Exception ex)
{
return false;
}