110,535
社区成员
发帖
与我相关
我的任务
分享
protected void AddExcel(DataSet ds)
{
DataTable dt = ds.Tables[0];
string fileName = Guid.NewGuid() + ".xls";
Excel.Application excel = new Excel.ApplicationClass();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
foreach (DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
{
excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
}
}
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
//excel.Save(fileName);
excel.Quit();
excel = null;
GC.Collect();//垃圾回收
}
for (int i = 0; i < dgv.Rows.Count; i++)
{
if (dgv.Rows[i].Cells[0].Value != null && (bool)dgv.Rows[i].Cells[0].Value == true)
{
//删除代码
}
}
class DGVColumnHeader : DataGridViewColumnHeaderCell
{
private Rectangle CheckBoxRegion;
private bool checkAll = false;
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
CheckBoxRegion = new Rectangle(
cellBounds.Location.X + 3,
cellBounds.Location.Y + 2,
25, cellBounds.Size.Height - 4);
if (this.checkAll)
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Normal);
Rectangle normalRegion =
new Rectangle(
cellBounds.Location.X + 1 + 25,
cellBounds.Location.Y,
cellBounds.Size.Width - 26,
cellBounds.Size.Height);
graphics.DrawString(value.ToString(), cellStyle.Font, new SolidBrush(cellStyle.ForeColor), normalRegion);
}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//Convert the CheckBoxRegion
Rectangle rec = new Rectangle(new Point(0, 0), this.CheckBoxRegion.Size);
this.checkAll = !this.checkAll;
if (rec.Contains(e.Location))
{
this.DataGridView.Invalidate();
}
base.OnMouseClick(e);
}
public bool CheckAll
{
get { return this.checkAll; }
set { this.checkAll = value; }
}
}