winform 导出execl 设置问题

Fry_cici 2010-12-24 11:33:30
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;
}
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-12-24
  • 打赏
  • 举报
回复
设置Excel表单元格边框

EXCEL
Fry_cici 2010-12-24
  • 打赏
  • 举报
回复
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;



这个的解释
如若初见 2010-12-24
  • 打赏
  • 举报
回复
什么情况?
Fry_cici 2010-12-24
  • 打赏
  • 举报
回复
如何设置19行 每行每列的格式?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧