using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using EXCELAPPLICATION = Microsoft.Office.Interop.Excel.Application;
using System.Xml;
string xmlpath = System.AppDomain.CurrentDomain.BaseDirectory + "settings.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlElement root = doc.DocumentElement;
XmlElement major = (XmlElement)root.GetElementsByTagName("major")[0];
string majorstr = major.InnerText;
EXCELAPPLICATION EXCELA = new EXCELAPPLICATION();
if(EXCELA ==null)
{
MessageBox.Show("无法创建EXCEL对象,您的电脑可能未安装excel");
}
try
{
string filename = "";
string savefilename = "";
string tfname = "";
string tname = "";
if (majorstr == "结构专业")
{
if (txmlxstr == "人防")
{
tfname = "tp\\"+majorstr+"\\人防.xls";
tname = System.AppDomain.CurrentDomain.BaseDirectory + tfname;
}
else
{
MessageBox.Show("项目类型模板没有制定");
// tname = "Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet";
}
}
// MessageBox.Show(tname);
SaveFileDialog sd = new SaveFileDialog();
sd.DefaultExt = "xls";
sd.Filter = "Excel文件|*.xls";
sd.FileName = filename;
sd.ShowDialog();
savefilename = sd.FileName;
EXCELA.Visible = true;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbooks wbs = EXCELA.Workbooks;
Microsoft.Office.Interop.Excel.Workbook wb;
if (tfname=="")
{
wb = wbs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
}
else
{
wb = wbs.Add(tname);
}
// Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
//Range ec =EXCELA.Range[ws.Cells[1, 2], ws.Cells[1, 3]];
//ec.Application.DisplayAlerts = false;
//ec.Merge(missing);
//ec.Application.DisplayAlerts = true;
//Range ra = EXCELA.Range[ws.Cells[1, 5], ws.Cells[1, 7]];
//ra.Application.DisplayAlerts = false;
//ra.Merge(missing);
//ra.Application.DisplayAlerts = true;
//ws.Cells[1, 1] = "项目名称";
//ws.Cells[1, 2] = xmmc;
//ws.Cells[1, 4] = "子项名称";
//ws.Cells[1, 5] = zxmc;
int rc = dataGridView1.Rows.Count;
string[] codestr = new string[rc];
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
//for(int j = 0;j<dataGridView1.ColumnCount;j++)
//{
// ws.Cells[r + 3, j+1] = dataGridView1.Rows[r].Cells[j].Value;
//}
codestr[r] = dataGridView1.Rows[r].Cells[1].Value.ToString();
//for (int k = 0; k < rcode;k++ )
//{
// if (vcode[k]==codestr[r])
// {
// js[k] = js[k] + 1;
// }
//}
System.Windows.Forms.Application.DoEvents();
}
string[] vcode = codestr.Distinct().ToArray();
int rcode = vcode.Length;
string[] codestr2 = new string[rc];
string tcode = "";
int ts = 0;
//for (int r = 0; r < dataGridView1.Rows.Count; r++)
//{
// x = int.Parse(dataGridView1.Rows[r].Cells[4].Value.ToString());
// y = int.Parse(dataGridView1.Rows[r].Cells[5].Value.ToString());
// //for(int j = 0;j<dataGridView1.ColumnCount;j++)
// //{
// // ws.Cells[r + 3, j+1] = dataGridView1.Rows[r].Cells[j].Value;
// //}
// tcode = dataGridView1.Rows[r].Cells[1].Value.ToString();
// for (int k = 0; k < rcode; k++)
// {
// if (vcode[k] == tcode)
// {
// ts = js[k];
// }
// }
// string etx = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[x, y]).Text.ToString();
// ws.Cells[x, y] = etx+""+"*"+dataGridView1.Rows[r].Cells[3].Value.ToString()+"\n";
// ws.Cells[x,y+3]="";
// ws.Cells[x,y+3]=ts;
// ws.Cells[x, y] = dataGridView1.Rows[r].Cells[3].Value.ToString();
// System.Windows.Forms.Application.DoEvents();
//}
for (int m = 0; m< rcode; m++ )
{
string scode = vcode[m];
int x = 0;
int y = 0;
int js = 0;
string content_cs = "";
for (int i = 0; i < dataGridView1.Rows.Count;i++ )
{
tcode = dataGridView1.Rows[i].Cells[1].Value.ToString();
if(scode==tcode)
{
js = js + 1;
x = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
y = int.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString());
content_cs = content_cs +js.ToString()+"、"+ dataGridView1.Rows[i].Cells[3].Value.ToString()+"\n";
}
}
ws.Cells[x, y] = content_cs;
ws.Cells[x, y+4] = js;
System.Windows.Forms.Application.DoEvents();
}
ws.Columns.EntireColumn.AutoFit();
MessageBox.Show(filename + "保存成功", "提示", MessageBoxButtons.OK);
if(savefilename!="")
{
wb.Saved = true;
wb.SaveCopyAs(savefilename);
}
}
catch(Exception ex)
{
MessageBox.Show("导出文件出错\n"+ex.Message);
}
EXCELA.Quit();
GC.Collect();
}