Excel 导出

Excel 导出 

        /// <summary>
        /// 把dataGridView1中数据导出到Excel
        /// </summary>
        /// <param name="dataGridView1"></param>
        public static void ExportDataGridViewToExcel2(DataGridView dataGridView2)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl  files  (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出Excel文件到";

            DateTime now = DateTime.Now;
            saveFileDialog.FileName = "采购订单批导模板-" + now.Year.ToString().PadLeft(2)
            + now.Month.ToString().PadLeft(2, '0')
            + now.Day.ToString().PadLeft(2, '0') + "-"
            + now.Hour.ToString().PadLeft(2, '0')
            + now.Minute.ToString().PadLeft(2, '0')
            + now.Second.ToString().PadLeft(2, '0');

            //saveFileDialog.ShowDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }


            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            string str = "";
            int iss = 0;
            int isss = 0;
            int sIndex = 0;
            try
            {
                //写标题    
                for (int i = 0; i < dataGridView2.ColumnCount; i++)
                {
                    if (i > 0 && iss > 0)
                    {
                        str += "\t";
                    }
                    else if (dataGridView2.Columns[i].HeaderText.Equals("sizeName"))
                    {
                        str += "物料编码";
                        iss = 1;
                    }
                    else if (dataGridView2.Columns[i].HeaderText.Equals("a19"))
                    {
                        str += "交货日期";
                        iss = 1;
                    }
                    else if (dataGridView2.Columns[i].HeaderText.Equals("a20"))
                    {
                        str += "税码";
                        iss = 1;
                    }
                    else
                    {
                        iss = 0;
                    }
                }

                sw.WriteLine(str);
                //写内容  
                for (int j = 0; j < dataGridView2.Rows.Count; j++)
                {
                    string tempStr = "";
                    for (int k = 0; k < dataGridView2.Columns.Count; k++)
                    {
                        if (k > 0 && isss > 0)
                        {
                            tempStr += "\t";
                        }
                        if ((dataGridView2.Rows[j].Cells[k].Value == null) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                        {
                            tempStr += "";
                            //isss = 0;
                        }
                        else
                        {
                            if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 0) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                tempStr += "";
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 1) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                tempStr += "Z100";
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 2) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                tempStr += "";
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 13) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                //物料编码
                                //tempStr += "'" + dataGridView1.Rows[j].Cells[8].Value.ToString() + dataGridView1.Rows[j].Cells[10].Value.ToString() + dataGridView1.Rows[j].Cells[12].Value.ToString();
                                StringBuilder sb = new StringBuilder("=\"");
                                //sb.Append(dataGridView2.Rows[j].Cells[8].Value.ToString() + dataGridView2.Rows[j].Cells[10].Value.ToString() + dataGridView2.Rows[j].Cells[12].Value.ToString()).Append("\"");
                                sb.Append(dataGridView2.Rows[j].Cells[8].Value.ToString()).Append("\"");
                                tempStr += sb.ToString();
                                //sb.Remove(0, sb.Length);   //清除sb中的所有信息
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 17) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                //dataGridView1.Rows[j].Cells[10].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                                //网格值
                                //tempStr += "'" + dataGridView1.Rows[j].Cells[10].Value.ToString() + dataGridView1.Rows[j].Cells[12].Value.ToString();
                                StringBuilder sb1 = new StringBuilder("=\"");
                                sb1.Append(dataGridView2.Rows[j].Cells[10].Value.ToString() + dataGridView2.Rows[j].Cells[12].Value.ToString()).Append("\"");
                                tempStr += sb1.ToString();
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 18) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                tempStr += dataGridView2.Rows[j].Cells[16].Value.ToString();
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 19) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                DateTime nowTime = DateTime.Now;
                                int year = nowTime.Year;//年
                                int month = nowTime.Month;//月
                                int day = nowTime.Day;//日 
                                string nowdate = (year + "-" + month + "-" + day).ToString();
                                //交货日期
                                tempStr += nowdate;
                                isss = 1;
                            }
                            else if ((dataGridView2.Rows[j].Cells[k].ColumnIndex == 20) || (dataGridView2.Rows[j].Cells[k].Value.Equals(true)))
                            {
                                tempStr += "J1";
                                isss = 1;
                            }
                            else
                            {
                                isss = 0;
                            }
                        }
                    }
                    sw.WriteLine(tempStr);
                }
                sw.Close();
                myStream.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }
            MessageBox.Show("导出成功!", "导出提示");
        }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值