We propose an algorithm to construct an optimal H2-based representation of integral operators, which has both optimized H2-partition and minimized rank, for accelerating integral equation based solutions of large-scale electromagnetic problems. The computational cost of this algorithm is small, since the cost associated with each admissible block depends on the block rank only. The proposed algorithm is also kernel independent. With the proposed H2-representation, both H2-based fast direct and iterative solvers are developed. They significantly outperform state-of-the-art H2-based solvers in computational efficiency without sacrificing accuracy.